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

Source: Internet
Author: User
Tags pkill

General article:

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& nbsp 3776 pts/0    ss   11:38   0:02 bash smx       1827& nbsp 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& nbsp 0.0  0.6  41536 12620?         s    11:38   0:00 update-notifier ... smx      11953& nbsp 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 and pgrep slightly less than 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

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.