kill vs killall – difference and command usage

In order to know the difference between kill and killall commands we first need to make sure that we understand the basics behind processes on the Linux system. Process is an instance of a running program. Each process is assigned PID ( Process ID ) which is unique for each process and therefore, no two processes can be assigned same PID. Once the process is terminated the PID is available for reuse. Try the following example:

$ yes > /dev/null &
[1] 3706

The command above will start process syes and output its standard output to /dev/null. What we are interested in here, is the second line which contains the following information “[1]” ( job ID ) and “3706” the actual PID. On your Linux system you can run multiple process at any given time and each process, depending on the user privileges can be terminated using either kill or killall commands. Let’s start few additional processes:

$ yes > /dev/null &
[2] 3782
$ yes > /dev/null &
[3] 3783
$ yes > /dev/null &
[4] 3784
$ yes > /dev/null &
[5] 3785

From the above you can see that we have started additional processes using yes command and that each process have different PID. To list all you processes forked from the current shell use jobs command:

$ jobs
[1]   Running                 yes > /dev/null &
[2]   Running                 yes > /dev/null &
[3]   Running                 yes > /dev/null &
[4]-  Running                 yes > /dev/null &
[5]+  Running                 yes > /dev/null &

The difference between kill vs killall commands is that with kill command we can terminate only a single process at the time, whereas with killall command we are able to terminated multiple processes based on given criteria such as process group, process age or user ownership. Let’s use kill command to kill process with PID 3783:

$ kill 3783
[3]   Terminated              yes > /dev/null

Terminating each process one by one can prove to be a hard and tedious work. Let’s see whether we can get some help by using killall command and process name:

$ killall yes
[1]   Terminated              yes > /dev/null
[2]   Terminated              yes > /dev/null
[4]-  Terminated              yes > /dev/null
[5]+  Terminated              yes > /dev/null

As you can see all processes were terminated based on the process name.