1. Overview

There are several ways to kill a process – also called a task – and most of them involve running commands in the terminal.

In this tutorial, we’ll focus on ways that pertain to background processes.

2. Before We Start

2.1. The kill Command

The basic command used to kill a process in Linux is kill. This command works in conjunction with the ID of the process – or PID we want to end.

Besides the PID, we can also end processes using other identifiers, as we’ll see further down.

The kill command takes in a termination signal, which can have one of several values:

  • SIGINT (2) – has the same result as pressing Ctrl+C in a terminal; it doesn’t automatically end the process
  • SIGQUIT (3) – does the same thing as SIGINT, with the added benefit of producing a core dump
  • SIGKILL (9) – forces termination of the process; it can’t be ignored or gracefully shut down
  • SIGTERM (13) – terminates the process, but gives time to gracefully shut down

If we don’t specify a termination signal, the default SIGTERM (13) will be used.

2.2. Background vs. Foreground

Also, let’s keep in mind the difference between background and foreground processes.

foreground process is any command or process we run directly and wait for it to complete, such as:

$ ls -l
total 716
-rwxr-xr-x 1 root root 1563 oct 23 2016 espdiff
-rwxr-xr-x 1 root root 117104 sep 26 2019 gnome-mahjongg
-rwxr-xr-x 1 root root 125304 sep 13 2019 gnome-mines
-rwxr-xr-x 1 root root 178712 sep 10 2019 gnome-sudoku
-rwxr-xr-x 1 root root 302336 sep 15 2019 sol

A background process, on the other hand, is one that the shell does not wait on. To run a background process, we append “&” at the end of the process we want to start:

$ ./myScript.sh &

3. How to Actually Stop a Process

As we mentioned in the beginning, most of the time, we need the PID in conjunction with the kill command, but that is not the only way, as we’ll see.

3.1. ps

The ps command is used to list processes in Linux. Here, we’ll add -eaf to get the information we want:

$ ps -eaf
UID                      PID    PPID  C STIME TTY          TIME CMD
baeldung-reader         3208    2578  0 14:20 ?        00:00:01 /usr/share/skypeforlinux/sky
baeldung-reader         3226    2578  2 14:20 ?        00:00:06 /snap/slack/24/usr/lib/slack
baeldung-reader         3230    3208  0 14:20 ?        00:00:00 /usr/share/skypeforlinux/sky
baeldung-reader         3232    3230  0 14:20 ?        00:00:00 /usr/share/skypeforlinux/sky
baeldung-reader         3277    3208  0 14:20 ?        00:00:00 /usr/share/skypeforlinux/sky
baeldung-reader         3280    3208  0 14:20 ?        00:00:00 /usr/share/skypeforlinux/sky
baeldung-reader         3325    3208  3 14:20 ?        00:00:08 /proc/self/exe --type=render
baeldung-reader         3363    3226  0 14:20 ?        00:00:00 /snap/slack/24/usr/lib/slack
baeldung-reader         3398    3226  0 14:20 ?        00:00:00 /snap/slack/24/usr/lib/slack
baeldung-reader         3411    2578 12 14:20 ?        00:00:29 /opt/google/chrome/chrome
baeldung-reader         3417    3411  0 14:20 ?        00:00:00 cat
baeldung-reader         3418    3411  0 14:20 ?        00:00:00 cat
baeldung-reader         3423    3226  0 14:20 ?        00:00:00 /snap/slack/24/usr/lib/slack
baeldung-reader         3426    3411  0 14:20 ?        00:00:00 /opt/google/chrome/chrome --
baeldung-reader         3428    3411  0 14:20 ?        00:00:00 /opt/google/chrome/chrome --
baeldung-reader         3429    3428  0 14:20 ?        00:00:00 /opt/google/chrome/nacl_help
baeldung-reader         3432    3428  0 14:20 ?        00:00:00 /opt/google/chrome/chrome --
baeldung-reader         3434    3426  0 14:20 ?        00:00:00 /opt/google/chrome/nacl_help
baeldung-reader         3441    3208  0 14:20 ?        00:00:00 /usr/share/skypeforlinux/sky
baeldung-reader         3456    3226  5 14:20 ?        00:00:12 /snap/slack/24/usr/lib/slack
baeldung-reader         3482    3411  1 14:20 ?        00:00:03 /opt/google/chrome/chrome --
baeldung-reader         3531    3426  3 14:20 ?        00:00:09 /opt/google/chrome/chrome --
baeldung-reader         3533    3531  0 14:20 ?        00:00:00 /opt/google/chrome/chrome --
baeldung-reader         3603    3432  3 14:20 ?        00:00:07 /opt/google/chrome/chrome --
baeldung-reader         3617    3432  3 14:20 ?        00:00:08 /opt/google/chrome/chrome --
baeldung-reader         3631    3432  0 14:20 ?        00:00:01 /opt/google/chrome/chrome --
baeldung-reader         3870    3432  5 14:21 ?        00:00:11 /opt/google/chrome/chrome --
baeldung-reader         3892    3432 25 14:21 ?        00:00:57 /opt/google/chrome/chrome --
baeldung-reader         3905    2892  9 14:21 ?        00:00:22 ./jetbrains-toolbox --minimi

After running ps, we visually scan for the PID of the process we want to end, and run the kill command, passing in the PID:

$ sudo kill -9 3905

3.2. pgrep

The pgrep command finds processes by name and returns the PID for each match:

$ pgrep chrome
733
3638
11679
11736
11776
11848
11850
12193
12208
12240
12275
14705
17372
18003
18378
18526
18540
18736
22401
23844
$ sudo kill -9 733

3.3. pkill

pkill is one of the commands that can directly kill a background process, using the process name.

For example, if we want to stop all instances of RabbitMQ we can use:

$ sudo pkill rabbitmq

3.4. jobs

Another way to kill a process is by its job number. This is where the jobs command helps us.

First, let’s launch a couple of jobs:

$ sleep 100 &
[1] 24827
$ sleep 200 &
[2] 24832

Note that we see the job ID as well as the PID after each command.

We can use jobs to list them:

$ jobs
[1]-  Running                 sleep 100 &
[2]+  Running                 sleep 200 &

And then we can kill by the job ID:

$ kill %1 # Here 1 is the job number. We can also kill job number 2 if we want in the same way
$ jobs
[1]-  Terminated              sleep 100
[2]+  Running                 sleep 200 &

3.5. killall

In contrast to kill, which uses the PID of the process, the killall command takes the name and, as implied, kills all processes with that name.

This is useful, for example, when we have a frozen web browser (like Chrome or Firefox) with multiple tabs open. We can easily kill all processes running on the browser and close it by using:

$ killall chrome
$ killall firefox

4. Bonus

4.1. fg

fg is a command used to bring a background process to the foreground. Then, we can simply stop the process by using Ctrl+C:

$ sleep 100 &
[1] 25994
$ jobs
[1]+  Running sleep 100 &
$ fg 1
sleep 100
^C
$

4.2. System Monitor

Another way to end a process is by launching the System Monitor, the Linux equivalent of the Windows Task Manager. To launch it, we run:

$ gnome-system-monitor

If we don’t have it installed on our machine, then we install it using:

$ sudo apt-get install gnome-system-monitor

In the System Monitor, we can see a list of all the processes currently running. To kill a process, we navigate through that list, right-click the process, and choose the kill option.

5. Conclusion

In this article, we learned how to kill background processes in Linux.

We can kill a process in multiple ways using various commands from the Linux ecosystem. Also, if we don’t like typing in a terminal, we can always use the System Monitor instead.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.