N ways to kill a process (kill) under linux

First, use ps to view the process, as follows:

$ 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 the terminal:

$ kill -s 9 1827

Where -s 9 specifies that the signal passed to the process is 9, that is, to force the process to terminate as soon as possible. See the appendix for each termination signal and its function.

1827 is the PID of Firefox found by ps above.

It's simple, but there is a problem. It doesn't matter if there are fewer processes. If there are too many processes, it will be painful. Whether it is ps -ef or ps -aux, each time you need to find the process to be killed in a large series of process information. , all eyes are spent.

Advanced articles:

Improvement 1 :

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

Or too much typing?

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
Compared with pgrep, 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

 

Appendix: Various Signals and Their Uses

 

[1] Signal Description Signal number on Linux x86
SIGABRT Process aborted 6
SIGALRM Signal raised by alarm 14
SIGBUS Bus error: "access to undefined portion of memory object" 7
SIGCHLD Child process terminated, stopped (or continued*) 17
SIGCONT Continue if stopped 18
SIGFPE Floating point exception: "erroneous arithmetic operation" 8
SIGHUP Hangup 1
SEAL Illegal instruction 4
SIGINT Interrupt 2
SIGKILL Kill (terminate immediately) 9
SIGPIPE Write to pipe with no one reading 13
SO MUCH Quit and dump core 3
SIGSEGV Segmentation violation 11
SIGSTOP Stop executing temporarily 19
SIGTERM Termination (request to terminate) 15
SIGTSTP Terminal stop signal 20
SIGTTIN Background process attempting to read from tty ("in") 21
SIGTTOU Background process attempting to write to tty ("out") 22
SIGUSR1 User-defined 1 10
SIGUSR2 User-defined 2 12
SIGPOLL Pollable event 29
SIGPROF Profiling timer expired 27
SIGSYS Bad syscall 31
SIGTRAP Trace/breakpoint trap 5
SIGURG Urgent data available on socket 23
SIGVTALRM Signal raised by timer counting virtual time: "virtual timer expired" 26
SIGXCPU CPU time limit exceeded 24
SIGXFSZ File size limit exceeded 25

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326087034&siteId=291194637