1. Overview

Process management is a fundamental aspect of Linux Administration. In this article, we’ll have a look at how to kill processes using process names.

2. Setting up a Process to Kill

Let’s create a scenario where the sleep command is executed with dummy_process as its alias name:

[user@localhost ~]$ bash -c 'exec -a dummy_process sleep 40000' &

We verify that our process is running using the ps command:

[user@localhost ~]$ ps aux |grep dummy_process
user       10009  0.0  0.0   7284   728 pts/0    S    10:34   0:00 dummy_process 40000
user       10232  0.0  0.0  12112  1052 pts/0    R+   10:44   0:00 grep --color=auto dummy_process

3. Killing a Process

3.1. Kill a Process With the Help of /proc//stat

Consequently, we kill the process using either a symbolic or a numeric signal name. In our case, we omit the signal name because the signal name SIGTERM is the default signal sent to the process:

[user@localhost ~]$ pkill dummy_process

We see that the dummy_process wasn’t killed:

[user@localhost ~]$ ps aux |grep dummy_process
user       10009  0.0  0.0   7284   728 pts/0    S    10:34   0:00 dummy_process 40000
user       10582  0.0  0.0  12112   984 pts/0    S+   11:00   0:00 grep --color=auto dummy_process

The pkill command retrieves the name of a process by reading /proc/<process_id>/stat:

[user@localhost ~]$ cat /proc/10009/stat
10009 (sleep) S 3292 10009 3292 34816 10721 1077936128 270 0 0 0 0 0 0 0 20 0 1 0 2785965 7458816 182 18446744073709551615 94380410159104 94380410187168 140735499122208 0 0 0 0 0 0 1 0 0 17 0 0 0 0 0 0 94380412287824 94380412289152 94380438171648 140735499125715 140735499125735 140735499125735 140735499128809 0

Furthermore, the second word in parentheses is the process name that should be specified when running pkill:

[user@localhost ~]$ pkill sleep
[1]+  Terminated              bash -c 'exec -a dummy_process sleep 40000'

3.2. Kill a Process With the Help of pgrep

We can kill the dummy_process using its alias name. In fact, we do this by specifying the -f option, which allows us to match the given process name with the full process name.

Assuming that we’ve recreated the dummy_process:

[user@localhost ~]$ pkill -f dummy_process
[2]-  Terminated              bash -c 'exec -a dummy_process sleep 40000'

However, the pkill command is easy to mishandle as it will kill any process that matches the given process name. Let’s create two processes with the names dummy_process and important_dummy_process.

Let’s assume that important_dummy_process is a critical process that we don’t want to be terminated:

[user@localhost ~]$ ps aux |grep dummy
user       12764  0.0  0.0   7284   788 pts/0    S    12:29   0:00 dummy_process 40000
user       12774  0.0  0.0   7284   824 pts/0    S    12:30   0:00 important_dummy_process 40000
user       12793  0.0  0.0  12112  1092 pts/0    S+   12:30   0:00 grep --color=auto dummy
[user@localhost ~]$ pkill -f dummy_process
[2]   Terminated              bash -c 'exec -a dummy_process sleep 40000'
[3]-  Terminated              bash -c 'exec -a important_dummy_process sleep 40000'

We see that the given name dummy_process matched both processes and killed both processes as well. This can be very dangerous. We can use the pgrep command to sift through the various matched processes and kill a specific process using its unique id number:

user@localhost ~]$ pgrep -fa dummy_process
13005 important_dummy_process 40000
13012 dummy_process 40000
[user@localhost ~]$ kill 13012
[3]-  Terminated              bash -c 'exec -a dummy_process sleep 40000'

Furthermore, we can use the pidof command to identify the unique id number of the process to be killed:

[user@localhost ~]$ pidof dummy_process
13788
[user@localhost ~]$ pidof dummy_process | xargs kill
[3]-  Terminated              bash -c 'exec -a dummy_process sleep 40000'

In addition, we can specify the no-run-if-empty option. This will make sure that xargs doesn’t execute if no arguments are provided:

pidof dummy_process | xargs -r kill
[3]-  Terminated              bash -c 'exec -a dummy_process sleep 40000'

3.3. Kill Multiple Processes Using killall

 Let’s create multiple dd processes that copy nothing to nowhere and proceed by looking at what processes would be killed if we ran pkill:

[user@localhost ~]$ dd if=/dev/zero of=/dev/null &
[1] 3593
[user@localhost ~]$ dd if=/dev/zero of=/dev/null &
[2] 3601
[user@localhost ~]$ dd if=/dev/zero of=/dev/null &
[3] 3608
[user@localhost ~]$ dd if=/dev/zero of=/dev/null &
[4] 3615
[user@localhost ~]$ pgrep -fa dd
2 kthreadd
184 ipv6_addrconf
1067 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
1770 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
2018 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
2404 /usr/bin/dbus-daemon --session --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
2506 /usr/bin/dbus-daemon --config-file=/usr/share/defaults/at-spi2/accessibility.conf --nofork --print-address 3
2749 /usr/libexec/evolution-addressbook-factory
2773 /usr/libexec/evolution-addressbook-factory-subprocess --factory all --bus-name org.gnome.evolution.dataserver.Subprocess.Backend.AddressBookx2749x2 --own-path /org/gnome/evolution/dataserver/Subprocess/Backend/AddressBook/2749/2
3593 dd if=/dev/zero of=/dev/null
3601 dd if=/dev/zero of=/dev/null
3608 dd if=/dev/zero of=/dev/null
3615 dd if=/dev/zero of=/dev/null

On the other hand, the killall command provides a safer option for killing multiple processes by their name:

[user@localhost ~]$ dd if=/dev/zero of=/dev/null &
[1] 3324
[user@localhost ~]$ dd if=/dev/zero of=/dev/null &
[2] 3331
[user@localhost ~]$ dd if=/dev/zero of=/dev/null &
[3] 3338
[user@localhost ~]$ dd if=/dev/zero of=/dev/null &
[4] 3345
[user@localhost ~]$ killall dd
[1]   Terminated              dd if=/dev/zero of=/dev/null
[2]   Terminated              dd if=/dev/zero of=/dev/null
[3]-  Terminated              dd if=/dev/zero of=/dev/null
[4]+  Terminated              dd if=/dev/zero of=/dev/null

4. Conclusion

In summary, we learned how to use the pkill command and when not to use it. Additionally, we made use of the kill command to terminate processes using a unique process id.

Moreover, we looked at how to kill multiple processes in a precautionary manner by using killall.