linux kills processes in multiple ways
- 2020-05-09 19:44:09
- OfStack
Routine report:
First, view the process with ps, as follows:
$ ps -ef
$ ps -ef
...
smx 1822 1 0 11:38 ? 00:00:49 gnome-terminal
smx 1823 1822 0 11:38 ? 00:00:00 gnome-pty-helper
smx 1824 1822 0 11:38 pts/0 00:00:02 bash
smx 1827 1 4 11:38 ? 00:26:28 /usr/lib/firefox-3.6.18/firefox-bin
smx 1857 1822 0 11:38 pts/1 00:00:00 bash
smx 1880 1619 0 11:38 ? 00:00:00 update-notifier
...
smx 11946 1824 0 21:41 pts/0 00:00:00 ps -ef
Or:
$ ps -aux
...
smx 1822 0.1 0.8 58484 18152 ? Sl 11:38 0:49 gnome-terminal
smx 1823 0.0 0.0 1988 712 ? S 11:38 0:00 gnome-pty-helper
smx 1824 0.0 0.1 6820 3776 pts/0 Ss 11:38 0:02 bash
smx 1827 4.3 5.8 398196 119568 ? Sl 11:38 26:13 /usr/lib/firefox-3.6.18/firefox-bin
smx 1857 0.0 0.1 6688 3644 pts/1 Ss 11:38 0:00 bash
smx 1880 0.0 0.6 41536 12620 ? S 11:38 0:00 update-notifier
...
smx 11953 0.0 0.0 2716 1064 pts/0 R+ 21:42 0:00 ps -aux
At this point, if I want to kill the firefox process, I type in:
$ kill -s 9 1827
Where -s 9 specifies that the signal to be passed to the process is 9, that is, to force and terminate the process as soon as possible. The termination signals and their functions are shown in the appendix.
1827 is the PID version of firefox found by ps above.
Simple, but there is a problem, less process does not matter, more process, it will feel painful, whether it is ps-ef or ps-aux, every time to find the process to kill in a series of process information, the eye is spent.
Advanced article:
Improvement of 1:
Pipe ps's query results to grep to find the process that contains the particular string. The pipe character "|" is used to separate two commands. The output of the command to the left of the pipe character is used as the input of the command to the right of the pipe character.
$ ps -ef | grep firefox
smx 1827 1 4 11:38 ? 00:27:33 /usr/lib/firefox-3.6.18/firefox-bin
smx 12029 1824 0 21:54 pts/0 00:00:00 grep --color=auto firefox
This time it's clean. And then there is
$kill -s 9 1827
Improvement 2 -- using pgrep:
1 what's the first thing that comes to mind when you see pgrep? That's right, grep! p of pgrep indicates that this command is dedicated to grep for process queries.
$ pgrep firefox
1827
What do you see? That's right, PID for firefox, and I'm going to type again:
$kill -s 9 1827
Improvement 3 -- using pidof:
What comes to mind when you see pidof? That's right, pid of xx, which literally translates to PID of xx.
$ pidof firefox-bin
1827
Slightly less than pgrep, pidof must give the full name of the process. And then there's the cliche:
$kill -s 9 1827
Whether you use ps and then slowly find the process PID or grep to find the process containing the corresponding string, or pgrep to directly find the process PID containing the corresponding string, and then manually input to kill, it is a bit troublesome. Is there a more convenient way? There are!
Improved 4:
$ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -s 9
Description:
The output of "grep firefox" is all processes that contain the keyword "firefox".
"grep-v grep" is the process that removes the keyword "grep" from the listed process.
"cut-c 9-15" intercepts the input line from the 9th character to the 15th character, which happens to be the process number PID.
The xargs command in "xargs kill-s 9" is used to take the output of the previous command (PID) as an argument to the "kill-s 9" command and execute it. "kill-s 9" will force the specified process to be killed.
Don't you want to complain about something? Yeah, it's too long
Improved 5:
If you know pgrep and pidof, why do you have to type a long string?
$ pgrep firefox | xargs kill -s 9
Improved 6:
$ ps -ef | grep firefox | awk '{print $2}' | xargs kill -9
kill: No such process
There is a more frustrating place, the process has been correctly found and terminated, but the execution is not found the process.
The function of awk '{print $2}' is to print (print) out the contents of column 2. As you can see from the general article, column 2 of ps output is exactly PID. The corresponding PID of the process is passed to kill via xargs as a parameter to kill the corresponding process.
To improve the 7:
Does xargs have to be called every time to pass PID to kill? The answer is no:
$kill -s 9 `ps -aux | grep firefox | awk '{print $2}'
Improve 8:
Yes, the command is still a little long, but pgrep.
$kill -s 9 `pgrep firefox`
Improved 9 - pkill:
What comes to mind when you see pkill? That's right, pgrep and kill! pkill = pgrep + kill.
$pkill -9 firefox
Note: "-9" means the signal sent is 9. The difference between pkill and kill in this point is that pkill does not need "s" and the termination signal level follows directly after "- ". I thought 1 was "-s 9", but I couldn't kill the process every time I ran it.
Improved 10 - killall:
killall and pkill are similar, although killall will report an error if the process name given is incomplete. Either pkill or pgrep can terminate a process by giving it the first part of its name.
$killall -9 firefox