How to kill a process.

How To Kill A Process.


In this article, we will show you how to use kill, killall, and pkill commands to terminate a process.

The main difference between these tools is that kill terminates processes based on Process ID number (PID), while the killall and pkill commands terminate running processes based on their names and other attributes.

 

System Kill Signals.

kill, killall, and pkill send a given signal to specified processes or process groups. When no signal is specified, each tool sends 15 (TERM).

The most commonly used signals are:

  • 1 (-HUP): to reload a process.

  • 9 (-KILL): to kill a process.

  • 15 (-TERM): to gracefully stop a process.

Signals can be specified in three different ways:

  • using a number (e.g., -1)

  • with the “SIG” prefix (e.g., -SIGHUP)

  • without the “SIG” prefix (e.g., -HUP).

Use the -l option to list all available signals:

kill -l  # or killall -l

 

Terminating Processes Using the kill Command

To terminate a process with the kill command first, you need to find the process PID. You can do this using different commands such as top, ps , pidof, and pgrep .

Let’s say the chrome browser has become unresponsive, and you need to kill the browser process. To find the process ID, use the pidof command:

pidof chrome

The command will print all chrome processes:

2551 2514 1963 1856 1771

Once you know the chrome processes PIDs to terminate all of them send the TERM signal:

kill -9 2551 2514 1963 1856 1771

 

Terminating Processes Using the killall Command

The killall command terminates all programs that match a specified name.

Using the same scenario as before, you can kill the chrome process by typing:

killall -9 chrome

killall accepts several options such as sending signals to processes owned by a given user, matching processes names against regular expressions, and the creation time. You can get a list of all options by typing killall (without any arguments) on your terminal.

For example, to terminate all processes running as a user “sara”, you would run the following command:

sudo killall -u sara

 

Terminating Processes Using the pkill Command

pkill terminates processes that match the pattern given on the command line:

pkill -9 chrome

The name of the process doesn’t have to be an exact match.

With pkill you can also send a signal to processes that are owned by a given user. To kill only the chrome processes owned by the user “sara”, you would type:

pkill -9 -u sara chrome

 

Conclusion

Terminating unresponsive programs using the kill, killall and pkill commands is an easy task. You only need to know the process name or PID.