The Linux command line provides various commands to terminate the process. For example, the "kill" command can be used to kill a process by passing its PID as a parameter, and the "pkill" command takes a pattern as input and kills all matching running processes. However, there is a command 'kill', which exactly matches the parameter name by default and terminates the matching process.
In this article, we will discuss this command through some practical examples. The kill command can be used to signal a particular process by using its name. Kill will kill all five versions of your program if you run the same command.
This signal can be specified as a parameter of this command, otherwise SIGTERM is sent by default. A process can generate child processes that are independent of the parent process, which means that each child process is identified by its pid. Using the kill Command, we use pid to select the child process to be killed, but using the kill Command, by indicating the name, because all child processes have the same name as the parent process, all processes will be killed.
Let's discuss the use of the kill command through some practical examples.
1) List all supported signals
The Kill Command supports some signals that can be listed with the - l option. Kill sends a signal to the process. You can use this - s option (followed by the signal name) to send a specific signal to the 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 the process. If there is no signal, the kill command is given. Both users and system administrators can use the above signals. So killall supports all these signals.
2) Kill a specific process
To kill a process, we need to identify it to check whether it is running. We can kill a specific process by its name. Suppose that there are two processes with the same initial characters:
iPhone 13 / Pro / Max: tips and tricks
$ 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 you can kill 'test' with the kill command_ again' :
$ killall test_again
[2]+ Terminated ./test_again
As you can see, the Kill Command terminates the "test_again" process. This can also be confirmed by ps command:
$ ps -aef | grep "test"
himanshu 3969 2811 0 14:14 pts/0 00:00:00 ./test
'test 'observed_ Again 'is not shown in the output because it was killed.
3) Kill a process, ignoring case
The kill 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 lowercase. Let's try to kill the ". / test" process, but let's enter the name in uppercase letters
$ killall TEST
TEST: no process found
So you can see that the kill command cannot find any process named test, and the process named 'test' is already running. To ensure that the Kill Command ignores case, use this - I option. Here is an example:
$ killall -I TEST
[1]- Terminated ./test
Notice that it now successfully terminates the "test" process.
4) Confirm termination process
The kill command can be used to kill multiple processes.
$ killall test test_again
[2]- Terminated ./test_again
[3]+ Terminated ./test
However, an error may occur when inserting a process name by indicating another name. If the indicated process is a key process of the system, it may be a serious problem. Therefore, it is recommended to use kill to interactively terminate the process through request confirmation. To do this, you can use this -i option.
$ killall -i test test_again
Kill test(4201) ? (y/N) y
Kill test_again(4202) ? (y/N) y
[1]- Terminated ./test
[2]+ Terminated ./test_again
So you can see that in this way, the user can use the kill command to control the termination of the process.
5) Kill the process quietly
Sometimes when killall cannot find the specified process, it complains about the same content in the output.
Here is an example:
$ killall TEST
TEST: no process found
However, if you want killall to perform its work quietly, you can use the following -q options:
$ killall -q TEST
$
So you can see that when you use - q, the output is suppressed.
6) Strange result of the Kill Command
The man page of the Kill Command says that by default, it matches only full names that are less than or equal to 15 characters in length. Therefore, if the command name exceeds 15 characters, the full name may not be available (i.e. swapped out). In this case, killall will kill everything that matches within the first 15 characters.
For example, suppose there are 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 name of the first process in the above output has exactly 15 characters. Let's try killing it with the Kill Command:
$ killall test_abcdefghij
[1]- Terminated ./test_abcdefghij
So you can see that the Kill Command successfully killed the process.
Now, according to the man page, if two names match more than 15 characters, killall will terminate the process of the name followed by the - e option, otherwise it will terminate all matches in 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
Notice that the names of both processes now have more than 15 matching characters. Now, when I try to kill the second process with kill:
$ killall test_abcdefghijklmnopqrstuvwxyz
[6]+ Terminated ./test_abcdefghijklmnopqrstuvwxyz
It only kills the specified process, not 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'm not sure if there's an error on my side or in the kill command. I would appreciate it if you could put forward your views on this in your comments.
By the way, here are the details of the kill 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.
conclusion
Starting with Centos 7, it encourages running pkill instead of kill, so by default, when you run a command, you may receive a "kill: command not found" error. If you still need it, install pkill (# yum install psmisc) by adding the psmisc package.