10 ways to kill a process in Linux

Source: Internet
Author: User
Tags file size time limit pkill

General article:

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

The code is 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:

The code is as follows:

$ 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:

The code is as follows:

$ 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.

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.

The code is as follows:

$ 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

The code is as follows:

$kill-S 9 1827

Improved 2--use Pgrep:

What's 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.

The code is as follows:

$ pgrep Firefox

1827

What do you see? Yes, Firefox's PID, and then you're typing:

The code is as follows:

$kill-S 9 1827

Improved 3--use PIDOF:

What do you think of pidof? The PID of XX, the literal translation is XX's PID.

The code is as follows:

$ 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é:

The code is as follows:

$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. Is there a more convenient way?

Improvement 4:

The code is as follows:

$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:

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

The code is as follows:

$ pgrep Firefox | Xargs Kill-s 9

Improvement 6:

The code is as follows:

$ 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 no:

The code is as follows:

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

Improvement 8:

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

The code is as follows:

$kill-S 9 ' pgrep Firefox '

Improved 9--pkill:

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

The code is as follows:

$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.

The code is as follows:

$killall-9 Firefox

Appendix: Various signals and their uses

The code is as follows:

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") 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

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.