N methods for killing processes in Linux

Source: Internet
Author: User

N methods of Kill processes in Linux: first, use ps to view processes. The method is as follows: $ ps-ef ...... Smx 1822 1 0? 00:00:49 gnome-terminalsmx 1823 1822 0? 00:00:00 gnome-pty-helpersmx 1824 1822 0 00:00:02 pts/0 bashsmx 1827 1 4? 00:26:28/usr/lib/firefox-3.6.18/firefox-binsmx 1857 1822 0 00:00:00 pts/1 bashsmx 1880 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-terminalsmx 1823 0.0 0.0 1988? S gnome-pty-helpersmx 1824 0.0 0.1 6820 pts/0 Ss 02 bashsmx 3776 1827 4.3 5.8 398196? Sl/usr/lib/firefox-3.6.18/firefox-binsmx 1857 0.0 0.1 6688 3644 pts/1 Ss bashsmx 1880 0.0 0.6 41536 12620? S update-notifier ...... Smx 11953 0.0 0.0 2716 1064 pts/0 R + p.m. ps-aux at this time, if I want to kill the Firefox process, input the following on the terminal: $ kill-s 9 1827 where-s 9 specifies that the signal sent to the process is 9, that is, to force and terminate the process as soon as possible. For the termination signals and their functions, see the appendix. Advanced article: Improvement 1: query results of ps are routed to grep to find the process that contains 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 firefoxsmx 1827 1 4 11: 38? 00:27:33/usr/lib/firefox-3.6.18/firefox-binsmx 12029 1824 0 00:00:00 pts/0 grep -- color = auto firefox this time refreshing. Then there is $ kill-s 9 1827 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 firefox1827 what do you see? That's right, Firefox's PID. Next we need to type: $ 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-bin1827 is slightly less than pgrep, pidof must give the full name of the process. Then let's talk about it as a saying goes: $ kill-s 9 1827, no matter whether you use ps and then slowly find the process PID or use grep to find the process that contains the corresponding string, you can also use pgrep to directly find the process PID containing the corresponding string, and then manually input it to kill it, which 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 Description: The output result of "grep firefox" is, 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: Know the pgrep and pidof commands. Why do you need to make such 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 has a depressing point. 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: Do I need 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: Yes, the command is still a little long and changed to pgrep. $ Kill-s 9 'pgrep firefox 'Improved 9 -- pkill: What did pkill think? That's right, pgrep and kill! Pkill = pgrep + kill. $ Pkill-9 firefox Description: "-9" means that the sent signal is 9. The difference between pkill and kill is that pkill does not require "s ", the termination signal level follows "-" directly. Previously I thought it was "-s 9", and the process cannot be terminated every time I run it. Improvement 10 -- killall: killall is similar to pkill. 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 Appendix: various signals and their purposes 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 hour 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.