The way to kill a process under Linux __linux

Source: Internet
Author: User
Tags pkill

General article

First, look at the process with PS by using the following methods:

$ 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 on the terminal input:

$ kill-s 9 1827

Where-s 9 the signal passed to the process is 9, that is, forcing, terminating the process as soon as possible. The 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 less, the process is much, it will feel pain, whether it is ps-ef or ps-aux, each time in a large number of process information inside find to kill the process, look at the eyes are spent.


Advanced Chapter

Improvement 1:

The PS query results are piped to grep to find the process containing the specific string. Pipe character "|" Used to separate two commands, the output of the command on the left side of the pipe is entered as the command to the right of the pipe character.

$ 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 it's

$kill-S 9 1827

or too much typing.


Improved 2--use Pgrep:

The first thing that comes to mind when you see pgrep. Yes, grep. Pgrep's p indicates that this command is dedicated to grep for process queries.

$ pgrep Firefox
1827

See what. Yes, Firefox's PID, and then you're typing:

$kill-S 9 1827


Improved 3--use pidof:

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

$ pidof Firefox-bin
1827
What is slightly less than pgrep is that pidof must give the full name of the process. And then there's the cliché:

$kill-S 9 1827

Whether using PS and then slowly looking for process PID or using grep to find the process containing the corresponding string, or by pgrep directly to find the process PID containing the corresponding string, and then manually input to kill, are slightly troublesome. There is no more convenient way. Yes.


Improvement 4:

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

Description

The output of "grep Firefox" is all the 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 of the Intercept input line to the 15th character, which happens to be the process number PID.

The Xargs command in "Xargs kill-s 9" is used to take the output (PID) of the previous command as an argument to the "Kill-s 9" command and execute the command. "Kill-s 9" will be forced to kill the specified process.

Don't you want to complain about something. Yes, it's too long.


Improvement 5:

Knowing Pgrep and pidof two orders, why do you have 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 depressing place, the process has been correctly found and terminated, but the execution was prompted to not find the process.

One of the functions of awk ' {print $} ' is to print (print) 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 corresponding PID of the process is passed to kill as parameter by Xargs, and the corresponding process is killed.


Improvement 7:

Does every time call Xargs to pass the PID to kill. The answer is in the negative:

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


Improvement 8:

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

$kill-S 9 ' pgrep Firefox '


Improved 9--pkill:

See what Pkill thought. 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 termination signal level directly following the "-" behind. I always thought it was "-s 9", and the result was that the process could not be terminated at each run.


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 part of the process name is given.

$killall-9 Firefox


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

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.