Linux command line offers various commands to kill a process. For example, the 'kill' command can be used to kill a process by passing its PID as an argument, the 'pkill' command takes a pattern as an input and all the matching running processes are killed. But there exists a command 'killall', which exactly matches the argument name by default, and kills the matching process. In this article, we will discuss this command with some practical examples.
The killall command can be used to send a signal to a particular process by using its name. It means if you have five versions of the same program running, the killall command will kill all five. The signal can be specified as an argument to this command or else SIGTERM is sent by default. A process can generate child processes which are independent of the parent process, it means that each sub-processes are identifying by its pid. With kill command, we use the pid to select the sub-process to kill but with killall command, by indicating the name, because all the sub-processes and process parent have the same name, all the processes will be killed.
Lets discuss the usage of this command through some practical examples.
1. List all the supported signals
Kill command support some signals which can be listed with
-l option. killall sends signals to processes. You can use the
-s option (followed by the signal name) to send a particular signal to a process.
$ killall -l HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS UNUSED
By default SIGTERM/TERM (15) is sent to process, if not signal is given to the killall command. The signals above are usefully used by both users and sysadmins. So killall supports all these signals.
2. Kill a particular process
To kill a process we need to identify it to check if it's running. We can kill a particular process by its name. Suppose, there are two processes that have same initial characters :
$ ps -aef | grep "test" himanshu 3969 2811 0 14:14 pts/0 00:00:00 ./test himanshu 3970 2811 0 14:14 pts/0 00:00:00 ./test_again
Now, here is how you can use the killall command to kill 'test_again' :
$ killall test_again + Terminated ./test_again
As you can see, the killall command terminated the 'test_again' process. This can also be confirmed through the ps command :
$ ps -aef | grep "test" himanshu 3969 2811 0 14:14 pts/0 00:00:00 ./test
Observe that 'test_again' is not displayed in the output as it is killed.
3. Kill a process ignoring case
The killall command is case-sensitive by default. Here is an example :
$ ps -aef | grep "test" himanshu 4177 3161 0 14:54 pts/3 00:00:00 ./test himanshu 4178 3161 0 14:54 pts/3 00:00:00 ./test_again himanshu 4180 3161 0 14:54 pts/3 00:00:00 grep --color=auto test
you can see that all our processes are in lower case. let us try to kill "./test" process but we enter the name in capital letter
$ killall TEST TEST: no process found
So you can see that the killall command could not find any process named TEST, while a process named 'test' is already running. To make sure that the killall command ignores the case, use the
-I option. Here is an example :
$ killall -I TEST - Terminated ./test
Observe that now it successfully terminated the 'test' process.
4. Kill processes with confirmation
The killall command can be used to kill more than process.
$ killall test test_again - Terminated ./test_again + Terminated ./test
But, it can happen to make a mistake when inserting the process name by indicating another name. It can be a serious problem if the indicated process is a critical process for the system. So it is recommended to use killall to terminate processes interactively by asking for a confirmation. To do it, you can use the
$ killall -i test test_again Kill test(4201) ? (y/N) y Kill test_again(4202) ? (y/N) y - Terminated ./test + Terminated ./test_again
So you can see that this way user can control the termination of processes using killall command.
5. Kill process quietly
Sometimes when killall is not able to find a specified process, it complains about the same in the output.
Here is an example:
$ killall TEST TEST: no process found
But, in case you want killall to carry out its work quietly, you can use the
$ killall -q TEST $
So you can see that when -q was used, the output was suppressed.
6. A strange result of killall command
The man page of the killall command says that by default, it matches complete names only if they are less than or equal to 15 characters in length. So, if a command name is longer than 15 characters, the full name may be unavailable (i.e. it is swapped out). In this case, killall will kill everything that matches within the first 15 characters.
For example, suppose there are following two processes with long names :
$ ps -aef | grep "test" himanshu 4021 3161 0 14:27 pts/3 00:00:00 ./test_abcdefghij himanshu 4035 3161 0 14:27 pts/3 00:00:00 ./test_abcdefgh
The first process in the output above has exactly 15 characters in name. Lets try to kill it using the killall command :
$ killall test_abcdefghij - Terminated ./test_abcdefghij
So you can see that the killall command kills the process successfully.
Now, according to the man page, if both the names would have had more than 15 matching characters, killall would have killed processes where the name is followed by
-e option or it will kill everything that matches within the first 15 characters. Here is an example :
$ ps -aef | grep "test" himanshu 4114 3161 0 14:40 pts/3 00:00:00 ./test_abcdefghijklmnopqrstuvwx himanshu 4141 3161 0 14:46 pts/3 00:00:00 ./test_abcdefghijklmnopqrstuvwxyz himanshu 4143 3161 0 14:46 pts/3 00:00:00 grep --color=auto test
Observe that both the processes now have more than 15 matching characters in their name. Now, when I tried to kill the second process using killall :
$ killall test_abcdefghijklmnopqrstuvwxyz + Terminated ./test_abcdefghijklmnopqrstuvwxyz
It killed only the specified process and not also the second process
$ ps -aef | grep "test" himanshu 4114 3161 0 14:40 pts/3 00:00:00 ./test_abcdefghijklmnopqrstuvwx himanshu 4146 3161 0 14:47 pts/3 00:00:00 grep --color=auto test
I am not sure if there is something incorrect on my side, or it is a bug in killall command. I'd appreciate if you put forward your views on this in comments.
BTW, here are the details of killall command on my system:
$ killall --version killall (PSmisc) 22.20 Copyright (C) 1993-2012 Werner Almesberger and Craig Small PSmisc comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under the terms of the GNU General Public License. For more information about these matters, see the files named COPYING.
kill command has some variety of command to kill process such as killall. Killall is helpful because we can kill a process by its name even if we can't remember its pid. It doesn't have some options because it is relatively simple to use