Home >Operation and Maintenance >Linux Operation and Maintenance >How to kill a process in linux
General:
First, use ps to view the process. The method is as follows:
Copy the code. The code is 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-HELER
MX 1822 0 11:38 PTS/0 00:00:02 bash
SMX 18271827 1 1 1 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:
Copy code The code is as follows:
$ ps -aux
……
smx 1822 0.1 0.8 58484 18152 ? sl 11:38 0:49 gnome-terminal
smx 1823 0.0 0.0 1988 71 2 ? 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
If I want to kill the Firefox process at this time, just enter in the terminal:
Copy code The code is as follows:
$ kill -s 9 1827
Among them, -s 9 specifies that the signal passed to the process is 9, that is, forced and as soon as possible Terminate the process. See the appendix for each termination signal and its functions.
1827 is the pid of Firefox found by PS above.
Simple, but there is a problem. If there are fewer processes, it doesn’t matter. If there are too many processes, it will be painful. Whether it is ps -ef or ps -aux, it will be in a large list of process information every time. I found the process to be killed and my eyes were dazzled.
Advanced article:
Improvement 1:
Pipe the ps query results to grep to find processes containing specific strings. The pipe character "|" is used to separate two commands. The output of the command on the left side of the pipe character will be used as the input of the command on the right side of the pipe character.
Copy code The code is as follows:
$ 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
It’s refreshing this time. Then
Copy the code. The code is as follows:
$kill -s 9 1827
Improvement 2——Use pgrep:
As soon as I see it What's the first thing that comes to mind with pgrep? That’s right, grep! The p of pgrep indicates that this command is grep specifically for process query.
Copy code The code is as follows:
$ pgrep firefox
1827
What did you see? That's right, Firefox's pid, you have to type it again:
$kill -s 9 1827
Improvement 3 - Use pidof:
What do you think of when you see pidof? That's right, pid of xx, literally translated is the pid of xx.
Copy code The code is as follows:
$ pidof firefox-bin
1827
The slight disadvantage compared with pgrep is that pidof must Gives the full name of the process. Then there is the cliché:
Copy the code The code is as follows:
$kill -s 9 1827
Whether you use ps and then slowly find the process pid or use It is a little troublesome to grep to find the process containing the corresponding string, or to use pgrep to directly find the process pid containing the corresponding string, and then manually enter it for kill to kill. Is there a more convenient way? have!
Improvement 4:
Copy code The code is as follows:
$ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -s 9
Instructions:
The output of "grep firefox" is all processes containing the keyword "firefox".
"grep -v grep" is to remove the process containing the keyword "grep" from the listed processes.
"cut -c 9-15" intercepts the 9th to 15th characters of the input line, which happens to be the process number pid.
The xargs command in "xargs kill -s 9" is used to use the output result (pid) of the previous command as the parameter of the "kill -s 9" command and execute the command. "kill -s 9" will forcefully kill the specified process.
Don’t you want to complain about something? Yes, it’s too long
Improvement 5:
You know the pgrep and pidof commands, why do you need to type such a long string!
Copy code The code is as follows:
$ pgrep firefox | xargs kill -s 9
Improvement 6:
Copy the code The code is as follows:
$ ps -ef | grep firefox | awk '{print $2}' | xargs kill -9
kill: no such process
There is one What is more depressing is that the process has been found and terminated correctly, but after execution, it prompts that the process cannot be found.
The function of awk '{print $2}' is to print the contents of the second column. According to the general article, you can know that the second column output by ps is exactly pid. Pass the corresponding pid of the process to kill as a parameter through xargs, and kill the corresponding process.
Improvement 7:
Do we need to call xargs every time to pass the pid to kill? The answer is no:
Copy code The code is as follows:
$kill -s 9 `ps -aux | grep firefox | awk '{print $2}'
Improvement 8:
Yes, the command is still a bit long, replace it with pgrep.
Copy code The code is as follows:
$kill -s 9 `pgrep firefox`
Improvement 9——pkill:
What did you think of when you saw pkill? That's right pgrep and kill! pkill=pgrep kill.
Copy code The code is as follows:
$pkill -9 firefox
Explanation: "-9" means the signal sent is 9, pkill and kill The difference at this point is: pkill does not require "s", and the termination signal level directly follows "-". I always thought it was "-s 9" before, but the process failed to be terminated every time I ran it.
Improvement 10 - killall:
killall and pkill are similar, but if the given process name is incomplete, killall will report an error. pkill or pgrep can terminate a process simply by giving part of the process name.
Copy code The code is as follows:
$killall -9 firefox
The above is the detailed content of How to kill a process in linux. For more information, please follow other related articles on the PHP Chinese website!