n ways to Kill a process (kill) under "Go" Linux

Source: Internet
Author: User
Tags pkill

Original URL: http://blog.csdn.net/andy572633/article/details/7211546

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 Firefox, the process is entered in the terminal:

$ kill-s 9 1827

Where-s 9 has developed a signal to pass to the process is 9, that is, forcing, terminate the process as soon as possible. The various termination signals and their effects are shown in the appendix.

1827 is the above PS found in the Firefox PID.

Simple, but there is a problem, the process is not indifferent, the process is more, it will feel pain, whether it is ps-ef or ps-aux, each time in a large string of process information to find the process to kill, look at the eyes are spent.

Advanced article:

Improvement 1:

The query results from PS are piped to grep to find the process that contains the specific string. Pipe symbol "|" Used to separate two commands, the output from the left command of the pipe character is entered as the command to 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

It's refreshing this time. Then there is

$kill-S 9 1827

Or too much typing?

Improved 2--using pgrep:

What do you think first when you see Pgrep? Yes, grep!. Pgrep p indicates that this command is a grep dedicated to process queries.

$ pgrep Firefox
1827

What did you see? Yes, the PID of Firefox, and then typing again:

$kill-S 9 1827

Improved 3--using pidof:

See what pidof think? Yes, pid of xx, literally translated is xx pid.

$ pidof Firefox-bin
1827
Slightly less significant than pgrep is that pidof must give the full name of the process. Then there is the cliché:

$kill-S 9 1827

Whether using PS and then slowly finding the process PID or using grep to find the process containing the corresponding string, or using pgrep directly to find the process PID containing the corresponding string, and then manually input to kill, is a bit cumbersome. Is there a more convenient way? Yes!

Improvement 4:

$ps-ef | grep Firefox | Grep-v grep | Cut-c 9-15 | Xargs Kill-s 9

Description

The result of "grep Firefox" is that all processes that contain the keyword "Firefox".

Grep-v grep removes the process that contains the keyword "grep" in the listed process.

"Cut-c 9-15" is the 9th character to the 15th character of the input line, which is exactly the process number PID.

The Xargs command in "Xargs kill-s 9" is used to take the output (PID) of the preceding command as a parameter to 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, it's too long.

Improvement 5:

Know Pgrep and pidof two orders, why do you want to play so long a string!

$ pgrep Firefox | Xargs Kill-s 9

Improvement 6:

$ PS-EF | grep Firefox | awk ' {print $} ' | Xargs kill-9
Kill:no such process

There is a more depressed place, the process has been correctly found and terminated, but the execution is not prompted to find the process.

The role of awk ' {print $} ' is to print out the contents of the second column. According to the general article, you can know that the second column of PS output is exactly PID. The PID of the process corresponding to the Xargs passed to kill as parameters, kill the corresponding process.

Improvement 7:

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

$kill-S 9 ' Ps-aux | grep Firefox | awk ' {print $} '

Improvement 8:

Yes, the order is still a bit long and replaced by Pgrep.

$kill-S 9 ' pgrep Firefox '

Improved 9--pkill:

What did you see Pkill think of? Yes, Pgrep and kill!. Pkill=pgrep+kill.

$pkill-9 Firefox

Note: "9" is the signal sent is 9,pkill and kill in this difference is: Pkill no "s", the terminating signal level directly followed by "-". I always thought it was "-s 9", and the result was that each run could not terminate the process.

Improved 10--killall:

Killall and Pkill are similar, but if the given process name is incomplete, Killall will give an error. Pkill or pgrep can terminate a process as long as a part of the process name is given.

$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") 21st
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

n ways to Kill a process (kill) under "Go" Linux

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.