General article:
First, use PS to view the process as follows:
Copy CodeThe 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:
Copy CodeThe 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 Firefox, the process is entered in the terminal:
Copy CodeThe code is as follows:
$ 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.
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.
Copy CodeThe 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 there is
Copy CodeThe code is as follows:
$kill-S 9 1827
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.
Copy CodeThe code is as follows:
$ pgrep Firefox
1827
What did you see? Yes, the PID of Firefox, and then typing again:
Copy CodeThe code is as follows:
$kill-S 9 1827
Improved 3--using pidof:
See what pidof think? Yes, pid of xx, literally translated is xx pid.
Copy CodeThe code is as follows:
$ 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é:
Copy CodeThe code is as follows:
$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:
Copy CodeThe code is as follows:
$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!
Copy CodeThe code is as follows:
$ pgrep Firefox | Xargs Kill-s 9
Improvement 6:
Copy CodeThe code is as follows:
$ 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:
Copy CodeThe code is as follows:
$kill-S 9 ' Ps-aux | grep Firefox | awk ' {print $} '
Improvement 8:
Yes, the order is still a bit long and replaced by Pgrep.
Copy CodeThe code is as follows:
$kill-S 9 ' pgrep Firefox '
Improved 9--pkill:
What did you see Pkill think of? Yes, Pgrep and kill!. Pkill=pgrep+kill.
Copy CodeThe 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 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.
Copy CodeThe code is as follows:
$killall-9 Firefox
Appendix: Various signals and their uses
Copy CodeThe 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
10 ways to kill a process in Linux