N methods of adding ps to the Kill process in CentOS

Source: Internet
Author: User

N methods of adding ps to the Kill process in CentOS
General:

First, use ps to view the process as follows:

$ Ps-ef

......
Smx 1822 1 0? 00:00:49 gnome-terminal
Smx 1823 1822 0? 00:00:00 gnome-pty-helper
Smx 1824 1822 0 00:00:02 pts/0 bash
Smx 1827 1 4? 00:26:28/usr/lib/firefox-3.6.18/firefox-bin
Smx 1857 1822 0 00:00:00 pts/1 bash
Smx 1880 1619 0? 00:00:00 update-notifier
......
Smx 11946 1824 0 00:00:00 pts/0 ps-ef

Or:

$ Ps-aux

......

Smx 1822 0.1 0.8 58484 18152? Sl gnome-terminal
Smx 1823 0.0 0.0 1988 712? S gnome-pty-helper
Smx 1824 0.0 0.1 6820 pts/0 Ss bash
Smx 1827 4.3 5.8 398196 119568? Sl/usr/lib/firefox-3.6.18/firefox-bin
Smx 1857 0.0 0.1 6688 pts/1 Ss bash
Smx 1880 0.0 0.6 41536 12620? S update-notifier
......
Smx 11953 0.0 0.0 2716 pts/0 R + ps-aux

In this case, if I want to kill Firefox, input the following information on the terminal:

$ Kill-s 9 1827

Among them,-s 9 sets the signal sent to the process to be 9, that is, to force and terminate the process as soon as possible. For the termination signals and their functions, see the appendix.

1827 is the PID of Firefox found by ps above.

Simple, but there is a problem. It doesn't matter if there are fewer processes. If there are more processes, it will feel painful, whether it is ps-ef or ps-aux, every time, you need to find the process to be killed in a large string of process information.

Advanced:

Improvement 1:

The ps query results are routed to grep to find the process containing 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.

$ Ps-ef | grep firefox
Smx 1827 1 4? 00:27:33/usr/lib/firefox-3.6.18/firefox-bin
Smx 12029 1824 0 00:00:00 pts/0 grep -- color = auto firefox

This is refreshing. Then

$ Kill-s 9 1827

Is Typing too much?

Improvement 2 -- use pgrep:

What do you first think of when you see pgrep? That's right, grep! Pgrep p indicates that this command is specially used for process query grep.

$ Pgrep firefox
1827

What do you see? That's right, Firefox's PID. Now I have to type again:

$ Kill-s 9 1827

IMPROVEMENT 3 -- use pidof:

What do you think of pidof? That's right, pid of xx. the literal translation is xx's PID.

$ Pidof firefox-bin
1827
Compared with pgrep, pidof must provide the full name of the process. Then we will talk about it as follows:

$ Kill-s 9 1827

Whether you 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 and kill it, it is a little troublesome. Is there a more convenient way? Yes!

IMPROVEMENT 4:

$ Ps-ef | grep firefox | grep-v grep | cut-c 9-15 | xargs kill-s 9

Note:

The output of "grep firefox" is that all processes that contain the keyword "firefox.

"Grep-v grep" is a process that removes the keyword "grep" from the listed processes.

"Cut-c 9-15" refers to intercepting 9th to 15th characters of the input line, which is exactly the process id pid.

The xargs command in "xargs kill-s 9" is used to take the output result (PID) of the preceding command as the parameter of "kill-s 9" and execute this command. "Kill-s 9" will forcibly kill the specified process.

Don't you want to complain? That's too long.

Improvement 5:

I know the pgrep and pidof commands. Why do I have to make a long string!

$ 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 depressing point where the process has been correctly found and terminated, but after the execution, the system prompts that the process cannot be found.

Awk '{print $2}' prints the content of the second column. According to the general article, we can know that the second column of ps output is exactly PID. The corresponding PID of the process is passed to kill through xargs as a parameter to kill the corresponding process.

Improvement 7:

Is it necessary to call xargs every time to pass the PID to kill? The answer is no:

$ Kill-s 9 'ps-aux | grep firefox | awk '{print $2 }''

Improvement 8:

That's right. The command is still a little long and changed to pgrep.

$ Kill-s 9 'pgrep firefox'

Improvement 9 -- pkill:

What did pkill think? That's right, pgrep and kill! Pkill = pgrep + kill.

$ Pkill-9 firefox

Note: "-9" means that the sent signal is 9. The difference between pkill and kill is that pkill does not require "s". The terminating signal level is directly following. Previously I thought it was "-s 9", and the process cannot be terminated every time I run it.

Improvement 10 -- killall:

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.

$ Killall-9 firefox

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.