Linux_N ways to kill a process

via: http://blog.csdn.net/andy572633/article/details/7211546

 

N ways to kill a process under linux

 

Pipe the query results of ps to grep to find processes that contain a specific 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

 

It's refreshing this time. Then there is 

$kill -s 9 1827

 

Improvement 2 - use pgrep:

 

What is the first thing that comes to your mind when you see pgrep? That's right, grep! The p in pgrep indicates that the command is a grep for process queries.

$ pgrep firefox

1827

What did you see? That's right, the PID of Firefox, and then we have to type again: 

$kill -s 9 1827

 

Improvement 3 - use pidof:

What do you think of when you see pidof? That's right pid of xx, the literal translation is the PID of xx.

$ pidof firefox-bin

1827

A slight disadvantage compared to pgrep is that pidof must give the full name of the process. And then there's the cliché:

$kill -s 9 1827

 

Whether you use ps and then slowly find the process PID, use grep to find the process containing the corresponding string, or use pgrep to directly find the process PID containing the corresponding string, and then manually input it to kill to kill, it is a little troublesome. Is there a more convenient way? have!

 

Improvement 4:

$ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -s 9

 

illustrate:

The output of "grep firefox" is all processes with the keyword "firefox".

"grep -v grep" removes processes with the keyword "grep" from the listed processes.

"cut -c 9-15" is to intercept the 9th to 15th characters of the input line, which is exactly 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 forcibly kill the specified process.

 

Don't you want to complain about something? yes too long

 

Improvement 5:

Knowing the two commands pgrep and pidof, why do you have to type such a long list!

$ pgrep firefox | xargs kill -s 9

 

Improvement 6:

$ ps -ef | grep firefox | awk '{print $2}' | xargs kill -9

kill: No such process

There is a more depressing place, the process has been correctly found and terminated, but the process is not found after execution.

 

The function of awk '{print $2}' is to print (print) the contents of the second column. According to the general article, it can be known that the second column of the ps output is exactly the PID. Pass the corresponding PID of the process to kill as a parameter through xargs to kill the corresponding process.

 

Improvement 7:

Do I have to call xargs every time to pass the PID to kill? the answer is negative:

$kill -s 9 `ps -aux | grep firefox | awk '{print $2}'`

 

Improvement 8:

That's right, the command is still a bit long, replaced by pgrep.

$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.

 

$pkill -9 firefox

Explanation: "-9" means that the signal sent is 9. The difference between pkill and kill is that pkill does not need "s", and the termination signal level directly follows "-". I always thought it was "-s 9" before, and it failed to kill the process 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 kill a process by giving only part of the process name.

 

$killall -9 firefox

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327064722&siteId=291194637