Detail method of forcing kill process under Linux

  • 2020-11-25 07:46:41
  • OfStack

Routine report:

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

s 9 specifies that the signal to the process is 9, that is, to force and terminate the process as soon as possible. See appendix for termination signals and their effects.

1827 is PID for Firefox as found by ps above.

Simple, but there is a problem, less process does not matter, process more, will feel painful, whether it is ES28en-ES29en or ES30en-ES31en, every time in a large string of process information to find the process to kill, look at the eye is spent.

Advanced article:

Improvement of 1:

The result of ps's query is piped to grep to find processes that contain a particular string. The pipe character "|" is used to separate two commands, and the output of the command on the left of the pipe is used as input to the command on the right of the pipe.

$ 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 refreshing. And then there is

$kill -s 9 1827

Improvement 2 -- Using pgrep:

1 What first comes to mind when you see pgrep? Yes, grep! pgrep's p indicates that this command is specifically for process queries.

$ pgrep firefox
1827

What do you see? That's right PID for Firefox, and typing again:

$kill -s 9 1827

Improvement 3 -- Using pidof:

What do you think of 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

Either use ps and then slowly find the process PID or 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 enter it to kill

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 9th character of the input line to the 15th character, which happens to be the process number PID.

The xargs command in "xargs ES128en-ES129en 9" is used to take the output of the previous command (PID) as an argument to the "ES132en-ES133en 9" command and execute it. "kill-s 9" kills the specified process.

Improved 5:

When you know pgrep and pidof, why do you have to type one long string?
$ pgrep firefox | xargs kill -s 9

Improved 6:

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

kill: No such process

In one of the more frustrating cases, the process was correctly found and aborted, but when it finished executing it was prompted that the process could not be found.

The purpose of awk '{print $2}' is to print (print) the contents of column 2. Based on the General section, you can see that the second column of the ps output happens to be PID. The corresponding PID of the process is passed to kill via xargs as a parameter, killing the corresponding process.

To improve the 7:

Do you have to call xargs 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 bit long, pgrep.

$kill -s 9 `pgrep firefox`

Improved 9 - pkill:

What do you think of pkill? That's right pgrep and kill! pkill = pgrep + kill.

$pkill - 9 firefox

Note: "-9" means that the signal sent is 9. The difference between pkill and kill is that pkill does not need an "S" and the termination signal level follows directly after "- ". I thought 1 was "-ES211en 9", but each time I ran, I couldn't kill the process.

Improved 10 - killall:

killall and pkill are similar, but if the process name given is incomplete, killall will report an error. pkill or pgrep can terminate a process by giving only part 1 of the process name.

$killall -9 firefox

Appendix: Various signals and their USES

Signal Description Signal number on Linux x86[1]
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
SIGILL Illegal instruction 4
SIGINT Interrupt 2
SIGKILL Kill (terminate immediately) 9
SIGPIPE Write to pipe with no one reading 13
SIGQUIT 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


Related articles: