Linux kill的N種方法

常規篇:
首先,用ps查看進程,方法如下:
$ 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
或者:
$ 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

此時如果我想殺了火狐的進程就在終端輸入:
$ kill -s 9 1827
其中-s 9 制定了傳遞給進程的信號是9,即強制、儘快終止進程。各個終止信號及其作用見附錄。

進階篇:

改進1:
把ps的查詢結果通過管道給grep查找包含特定字符串的進程。管道符“|”用來隔開兩個命令,管道符左邊命令的輸出會作爲管道符右邊命令的輸入。
$ 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
這次就清爽了。然後就是
$kill -s 9 1827

改進2——使用pgrep:
一看到pgrep首先會想到什麼?沒錯,grep!pgrep的p表明了這個命令是專門用於進程查詢的grep。
$ pgrep firefox
1827
看到了什麼?沒錯火狐的PID,接下來又要打字了:
$kill -s 9 1827

改進3——使用pidof:
看到pidof想到啥?沒錯pid of xx,字面翻譯過來就是 xx的PID。
$ pidof firefox-bin
1827
和pgrep相比稍顯不足的是,pidof必須給出進程的全名。然後就是老生常談:
$kill -s 9 1827
無論使用ps 然後慢慢查找進程PID 還是用grep查找包含相應字符串的進程,亦或者用pgrep直接查找包含相應字符串的進程PID,然後手動輸入給kill殺掉,都稍顯麻煩。有沒有更方便的方法?有!

改進4:
$ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -s 9
說明:
“grep firefox”的輸出結果是,所有含有關鍵字“firefox”的進程。
“grep -v grep”是在列出的進程中去除含有關鍵字“grep”的進程。
“cut -c 9-15”是截取輸入行的第9個字符到第15個字符,而這正好是進程號PID。
“xargs kill -s 9”中的xargs命令是用來把前面命令的輸出結果(PID)作爲“kill -s 9”命令的參數,並執行該命令。“kill -s 9”會強行殺掉指定進程。
難道你不想抱怨點什麼?沒錯太長了

改進5:
知道pgrep和pidof兩個命令,幹嘛還要打那麼長一串!
$ pgrep firefox | xargs kill -s 9

改進6:
$ ps -ef | grep firefox | awk '{print $2}' | xargs kill -9
kill: No such process
有一個比較鬱悶的地方,進程已經正確找到並且終止了,但是執行完卻提示找不到進程。
其中awk '{print $2}' 的作用就是打印(print)出第二列的內容。根據常規篇,可以知道ps輸出的第二列正好是PID。就把進程相應的PID通過xargs傳遞給kill作參數,殺掉對應的進程。

改進7:
難道每次都要調用xargs把PID傳遞給kill?答案是否定的:
$kill -s 9 `ps -aux | grep firefox | awk '{print $2}'`

改進8:
沒錯,命令依然有點長,換成pgrep。
$kill -s 9 `pgrep firefox`

改進9——pkill:
看到pkill想到了什麼?沒錯pgrep和kill!pkill=pgrep+kill。
$pkill -9 firefox
說明:"-9" 即發送的信號是9,pkill與kill在這點的差別是:pkill無須 “s”,終止信號等級直接跟在 “-“ 後面。之前我一直以爲是 "-s 9",結果每次運行都無法終止進程。

改進10——killall:
killall和pkill是相似的,不過如果給出的進程名不完整,killall會報錯。pkill或者pgrep只要給出進程名的一部分就可以終止進程。
$killall -9 firefox

注意:
大部分人在寫Shell 過濾進程的時候 都會使用 grep 在 ps aux 的輸出結果中查找指定的進程,但此時也會把 grep 進程也顯示出來 比如查找 pptpd 進程,會匹配出來兩條: 
[root@selboo ~]# ps aux | grep pptp 
root 20191 0.0 0.2 5108 704 pts/2 R+ 16:58 0:00 grep pptp 
root 22054 0.0 0.1 1952 608 ? Ss Oct22 0:00 /usr/sbin/pptpd 

常見的防止grep進程出現的方法就是在對加一個管道 grep -v grep 進行過濾:
[root@selboo ~]# ps aux | grep pptp | grep -v grep 
root 22054 0.0 0.1 1952 608 ? Ss Oct22 0:00 /usr/sbin/pptpd 

還有一個更方便的方法是用 正則 grep [p]ptpd來搜索pptpd這個進程:
[root@selboo ~]# ps aux | grep [p]ptp 
root 22054 0.0 0.1 1952 608 ? Ss Oct22 0:00 /usr/sbin/pptpd 

雖然說它比 grep -v grep 也未必方便多少,但是如果用大量循環的監控腳本上,每次都能減少一次系統grep調用,也就是減少一次創建進程,雖然提升很小可以忽略不計,但是用在shell寫的監控腳本上來說多少還有點提升的,優化就是注重細節嘛。 

以下是執行五次測試結果: 

使用grep -v grep 方式 :
複製代碼代碼如下:

[root@selboo etc]# time for (( i=1; i<=200; i++ )) ;do ps aux | grep pptp | grep -v pptp &>/dev/null; done 
real 0m1.487s 0m1.475s 0m1.488s 0m1.497s 0m1.499s 
user 0m0.335s 0m0.328s 0m0.334s 0m0.326s 0m0.312s 
sys 0m0.766s 0m0.757s 0m0.772s 0m0.784s 0m0.795s 


使用正則方式 :
複製代碼代碼如下:

[root@selboo etc]# time for (( i=1; i<=200; i++ )) ;do ps aux | grep [p]ptp &>/dev/null; done 
real 0m1.306s 0m1.344s 0m1.303s 0m1.298s 0m1.329s 
user 0m0.343s 0m0.313s 0m0.326s 0m0.274s 0m0.322s 
sys 0m0.742s 0m0.801s 0m0.753s 0m0.798s 0m0.784s 



附錄:各種信號及其用途
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 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章