Kill Processes in Linux – Kill, Pkill, Killall Commands

The kill, pkill, and killall commands are used to terminate processes on a Linux system. They are default command line utilities that all pretty much do the same thing, but in different ways. Mastering these commands will allow a Linux user to exercise more control over their system, as they’ll have the ability to end (or kill) services and processes that are running.

This guide will show you how the commands work, and take you through some examples to learn the proper syntax. Let’s go kill some processes.

Types of Kill Signals

When you execute any of the three kill command against a process, what’s really happening is that it’s sending a TERM signal to the process. This is a “nice” way of asking the process to close. It tells the process to wrap up what it’s doing, and then close when it finishes. That means a process has time to close out connections, stop writing to log files, and shut down gracefully.

There are a lot of different signals you can send to a process, but the only ones you’re likely to need are TERM, KILL, and HUP. You can see a full list of available signals with the following command.

$ kill -l

 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

SIGTERM, SIGKILL, and SIGHUP

TERM is the default signal sent, and it will typically terminate a process instantly. That’s because it only takes a fraction of a second for most processes to finish what they’re doing and close. Processes and applications are designed to receive TERM signals and respond to them with a smooth shutdown.

You don’t need to specify and extra options to send a TERM signal. You just need the kill command and a process ID you want to kill.

$ kill 1234

Processes that get “hung up” and become unresponsive may not do anything when they receive a TERM signal. If you encounter a stubborn process that refuses to close, you can send a KILL signal instead, which will close the process immediately. Add the -KILL or -9 (its corresponding number in the list above) option to your command to send a KILL signal to a process.

$ kill -KILL 1234
or
$ kill -9 1234
When possible, you should opt for the TERM signal to kill a process. A good rule of thumb is to only use KILL signals on stubborn processes that are refusing to close. There’s no reason to use KILL otherwise, as it’s much better to give the process a chance to terminate properly, rather than abruptly killing it.

The other handy kill signal is HUP, which instructs a daemon to reload its configuration. This can be useful when you’ve updated a service’s configuration files but need to reload the process for the new changes to take effect. Send the HUP signal by using the following command syntax.

$ kill -HUP 1234
or
$ kill -1 1234

Terminate Processes With kill Command

The kill command will accept one or multiple process IDs as an argument. In order to find the ID of the process we wish to kill, we can use commands such as ps, pgrep, top, pidof, etc.

Step 1. Start by identifying the ID of the process you wish to kill. In this example, we’ll kill Firefox.

$ pidof firefox
42252 42251 42226 42150

Step 2. Now that we know the process IDs associated with Firefox, we can use the kill command to terminate them with a TERM signal.

$ kill 42252 42251 42226 42150

Step 3. You can use the pidof firefox command to make sure the process has closed successfully. If it’s still running, try sending a KILL signal to the processes by adding the -KILL or -9 option.

$ kill -KILL 42252 42251 42226 42150
or
$ kill -9 42252 42251 42226 42150

Terminate Processes With pkill Command

Example 1. The pkill command works almost exactly the same as kill, except it accepts the name (or part of the name) of a process you want to kill. Going back to our example of killing Firefox, here’s how you’d perform the job with pkill.

$ pkill firefox
The pkill command will kill all processes that match the process name – even names that just partially match. For example, pkill chrom will terminate Google Chrome and Chromium simultaneously, since both process names are matched.

Example 2. To force pkill to only match exact process names, use the -x option.

$ pkill -x firefox

Example 3. Use various signals with the pkill command just like you would for kill.

$ pkill -9 firefox
$ pkill -hup nginx
etc

Example 4. To get a preview for what processes pkill is going to terminate, you can run the pgrep command first.

$ pgrep firefox
48273

The output from pgrep indicates that pkill firefox will terminate process ID 48273.

Terminate Processes With killall Command

Example 1. The killall command works a lot like pkill, except it only accepts exact names. So, killall fire will not work. You would need to specify the full name of the process.

$ killall firefox

As the name implies, killall will kill all processes that match the process name you supply.

Example 2. -i is a handy option that will prompt for confirmation before killing a process. This helps you avoid accidentally terminating an extra process that you didn’t realize would be matched.

$ killall -i firefox
Kill firefox(48587) ? (y/N)

Example 3. Use various signals with the killall command just like you would for kill and pkill.

$ killall -9 firefox
$ killall -hup nginx
etc

Example 4. Use the -o option to instruct killall to terminate processes older than a certain age. Specify the age in units with s, m, h, d, w, M, y for seconds, minutes, hours, days, weeks, Months, and years respectively.

Kill processes older than 30 minutes.

$ killall -o 30m firefox

Kill processes older than 1 week.

$ killall -o 1w firefox

Note that the process name is optional. You can also kill all processes older or younger than a certain age. This command kills any process older than 1 month.

$ killall -o 1M

Example 5. Use the -y optionn to terminate processes younger than a certain age.

Kill processes younger than 30 minutes.

$ killall -y 30m firefox

Kill processes younger than 1 week.

$ killall -y 1w firefox

Example 5. Use the -u to kill only the processes that are owned by a particular user.

Kill all processes owned by user linuxnightly.

$ killall -u linuxnightly

Kill all firefox processes owned by user linuxnightly.

$ killall firefox -u linuxnightly

Example 6. Combine any number of options in your killall command.

$ killall firefox -i -9 -u linuxnightly -y 30m

Conclusion

The kill command proves essential for reining in the running processes on Linux. If you need to terminate many processes at once, or just don’t want to deal with process ID numbers, the pkill and killall commands can be useful. Keep the TERM, KILL, and HUP signals in mind, as you’re likely to encounter situations that require them.

If our content helped you, please consider buying us a coffee. We appreciate your support!

Buy me a coffee donation button

Leave a Comment

Your email address will not be published. Required fields are marked *