Linux kill, killall, kill-9

Source: Internet
Author: User
Tags pkill

1) how to view the process:

PS-EF or PS aux

Root 15087 0.0 0.0 0 0? S [kworker/]
Root 15219 0.0 0.0 0 0? S [kworker/1:0]
Root 15460 0.0 0.0 0 0? S [kworker/]
Homer 15572 23.0 5.8 791472 119788? SL/usr/lib/Firefox
Homer 15590 0.1 0.1 273796 3132? SL/usr/lib/at-spi2-core/AT-SPI-bus-Launcher
Homer 15620 0.0 0.0 22360 1268 pts/0 r + PS aux

2) Kill-9

Kill-S 9 15572

Among them,-S 9 sets the signal sent to the process to be 9, that is, force and terminate the process as soon as possible. 15572 is the PID of Firefox found by PS above.

It's easy, but there is a problem. It doesn't matter if the process is small, and if there are too many processes, it will feel painful, whether it's PS-EF or PS-Aux, every time I find the PID of the process to be killed in a large string of process information, I have spent all my eyes.

Usage Details:

Kill-9, this powerful and dangerous Command forces the process to terminate suddenly at runtime, and the process cannot be self-cleaned after it ends. The hazard is that the system resources cannot be released normally. It is generally not recommended unless other methods are ineffective.
When using this command, you must use PS-Ef to confirm that no zombie process is left. Only the parent process can be terminated to eliminate zombie processes. If the zombie process is adopted by init, the problem will be serious. Killing the INIT process means shutting down the system.
If there is a zombie process in the system, and its parent process is INIT, And the zombie process occupies a large amount of system resources, you need to restart the machine at some time to clear the process.

2.1) Improvement 1 -- grep

Pass the PS query result to grep through a pipeline to find the process that contains a specific string. The pipe operator "|" is used to separate two commands. The output of the command on the left of the pipe operator is used as the input of the command on the right of the pipe operator.

Example: Homer @ Ubuntu :~ $ PS-Aux | grep Firefox

Homer 15572 1.7 5.0 808644 103260? SL/usr/lib/Firefox
Homer 15735 0.0 0.0 13584 920 pts/0 S + grep -- color = auto Firefox

Find the Firefox process PID and enter: Kill-S 9 15572

2.2) Improvement 2 -- pgrep

Pgrep P indicates that this command is specially used for process query grep.

Example: Homer @ Ubuntu :~ $ Pgrep Firefox
15572

2.3) Improvement 3 -- pidof

Pidof command, indicating PID of XX, literally translated as XX PID

Example: Homer @ Ubuntu :~ $ Pidof Firefox
15572

2.4) improvement 4 -- grep + awk

Grep Firefox lists Firefox process information, and awk takes the second field, that is, PID

Example: Homer @ Ubuntu :~ $ PS-Ef | grep Firefox | grep-V grep | awk '{print $2 }'
15572

2.5) Improvement 5 -- kill + xargs

There are several ways to kill a process:

A)PS-Ef | grep Firefox | grep-V grep | awk '{print $2}' |XargsKill-S 9

B)PgrepFirefox | xargs kill-S 9

C)PidofFirefox | xargs kill-S 9

Or

D) Kill-s 9'PS-Ef | grep Firefox | grep-V grep | awk '{print $2 }''

E) Kill-s 9'PgrepFirefox'

F) Kill-s 9'PidofFirefox'

2.6) Improvement 6 -- pkill

Pkill is similar to pgrep, and pkill indicates pgrep + kill

Example: pkill Firefox


3) killall

Killall command to kill all processes in the same process group. It allows you to specify the name of the process to be terminated, rather than the PID.

Killall and pkill are similar. However, if the process name is incomplete, killall reports an error. Pkill or pgrep can terminate a process as long as a part of the process name is given.

Homer @ Ubuntu :~ $ Killall firefo
Firefo: No process found
Homer @ Ubuntu :~ $ Killall Firefox
Homer @ Ubuntu :~ $ Killall-9 Firefox


4) Kill

The safest way to kill a process is to simply use the kill command without a modifier or a flag.
Example: # Kill-PID
Note: For standard kill commands, the default signal (signal) number is 15, which can usually achieve the goal of terminating problematic processes and Releasing Process resources to the system. However, if a child process is started and only the parent process is killed, the child process is still running and consumes resources. To prevent these so-called "zombie processes", ensure that all child processes are killed before the parent process is killed.

5) Kill-l

Example: Kill-l PID

-L option to tell the kill command to end the process as if the user who started the process had logged out. When this option is used, the kill command also tries to kill the left child process. But this command is not always successful-you may still need to manually kill the child process and then kill the parent process.


6) kill-HUP

Sometimes you only want to simply stop and restart the process.
Example: # kill-HUP PID
This command causes Linux to shut down the slow execution process and then restart immediately. This command is convenient when you configure the application. You can execute this command when you need to restart the process after modifying the configuration file.


Appendix: various signals and their usage

Signal Description Signal Number on Linux x86
SIGABRT Process aborted 6
Sigalrm Signal raisedAlarm 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

Grep globalhandlerexceptionresolver.-NR -- exclude-Dir = ". SVN" -- Binary-files = without-match

Reference recommendations:

N methods to kill processes in Linux

How to kill processes in Linux (kill, killall)

Killing processes in Linux, using Fuser, kill, and pkill commands

Appendix: various signals and their usage

Signal Description Signal Number on Linux x86
SIGABRT Process aborted 6
Sigalrm Signal raisedAlarm 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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.