這篇文章給大家介紹怎么在Linux中使用killall命令終止進程,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
專注于為中小企業(yè)提供成都網(wǎng)站設計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)新豐免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
1、基本用法
假如我們 3 個進程在運行,分別是 hello1, hello2, hello3 ,現(xiàn)在我們想殺死 hello1 進程,可以直接使用如下方式:
killall hello1
運行的結果如下:
[alvin@VM_0_16_centos test]$ ps aux | grep hello alvin 12061 0.0 0.0 4152 344 pts/0 S 14:41 0:00 ./hello1 alvin 12074 0.0 0.0 4152 344 pts/0 S 14:41 0:00 ./hello2 alvin 12084 0.0 0.0 4152 340 pts/0 S 14:41 0:00 ./hello3 alvin 12089 0.0 0.0 112648 964 pts/0 R+ 14:41 0:00 grep --color=auto hello [alvin@VM_0_16_centos test]$ killall hello1 [1] Terminated ./hello1 [alvin@VM_0_16_centos test]$ ps aux | grep hello alvin 12074 0.0 0.0 4152 344 pts/0 S 14:41 0:00 ./hello2 alvin 12084 0.0 0.0 4152 340 pts/0 S 14:41 0:00 ./hello3 alvin 12170 0.0 0.0 112648 964 pts/0 R+ 14:42 0:00 grep --color=auto hello
可以看到,hello1 進程已經(jīng)被殺死了。
剩下的 hello2 和 hello3 進程,我們想一次性殺死他們,也就是批量殺死進程,可以如下操作:
[alvin@VM_0_16_centos test]$ killall hello* hello: no process found hello1: no process found hello.c: no process found [2]- Terminated ./hello2 [3]+ Terminated ./hello3
如此,以 hello 開頭的進程全部被干掉。
2、終止某個用戶所運行的進程
我們可以殺死以滿足某個正則表達式的一組進程,同樣的,我們也可以殺死某個用戶運行的所有進程。
比如,用戶 harry 現(xiàn)在運行如下幾個進程:
[alvin@VM_0_16_centos test]$ ps aux | grep harry root 13675 0.0 0.2 148236 5584 ? Ss 14:55 0:00 sshd: harry [priv] harry 13677 0.0 0.1 148236 2944 ? S 14:55 0:00 sshd: harry@pts/1 root 13678 0.0 0.2 148236 5444 ? Ss 14:55 0:00 sshd: harry [priv] harry 13680 0.0 0.1 148236 2252 ? S 14:55 0:00 sshd: harry@notty harry 13681 0.0 0.1 53228 2168 ? Ss 14:55 0:00 /usr/libexec/openssh/sftp-server harry 13694 0.0 0.1 116436 3252 pts/1 Ss+ 14:55 0:00 -bash harry 13948 0.0 0.0 4152 344 pts/1 S 14:57 0:00 ./hello1 harry 13952 0.0 0.0 4152 344 pts/1 S 14:57 0:00 ./hello2 harry 13959 0.0 0.0 4152 344 pts/1 S 14:57 0:00 ./hello3 alvin 14005 0.0 0.0 112648 964 pts/0 R+ 14:58 0:00 grep --color=auto harry
我們現(xiàn)在想殺死 harry 所運行的所有進程,可以以如下方式操作:
killall -u harry
運行結果如下:
[alvin@VM_0_16_centos test]$ sudo killall -u harry [alvin@VM_0_16_centos test]$ ps aux | grep harry alvin 14040 0.0 0.0 112648 964 pts/0 R+ 14:58 0:00 grep --color=auto harry
但是,這個選項要慎用,因為它會把該用戶所有進程,包括終端進程,全部殺死,將導致該用戶直接退出。所以,如果不想挨揍的話不要輕意嘗試這個選項。
3、終于時間的方式終止進程
假如我們現(xiàn)在運行了很多程序,我們只想殺死運行時間超過 5h 的進程,那么可以使用 -o 選項,其中 o 代表 older 如下:
killall -o 5h
同樣地,如果你想殺死進行時間小于 4h 的進程,那么可以使用 -y 選項,其中 y 代表 younger ,如下:
killall -y 4h
這兩個選項同樣非常粗暴,也會把終端退出,所以先不演示了。
4、忽略大小寫
默認情況下,killall 命令是大小寫敏感的,所以我們?nèi)绻麑戝e大小寫,將無法正確殺死進程。
[alvin@VM_0_16_centos test]$ killall HELLO1 TEST1: no process found
如果我們想忽略大小寫,可以加上 -I (大寫字母 i )選項。
[alvin@VM_0_16_centos test]$ killall -I HELLO1 [1] Terminated ./hello1
5、關閉命令執(zhí)行回顯
默認情況下,killall 會告訴你命令執(zhí)行情況,但是,我們?nèi)绻魂P心它的執(zhí)行結果,只想讓它靜默執(zhí)行,該怎么辦?只需加上 -q 選項即可,其中 q 表示 quite , 如下:
[alvin@VM_0_16_centos test]$ killall HELLO2 HELLO2: no process found [alvin@VM_0_16_centos test]$ killall -q HELLO2 [alvin@VM_0_16_centos test]$
6、列出所有支持的信號
如前文所述,默認情況下,killall 命令將發(fā)送 SIGTERM 信號,那么,安可以發(fā)送其它信號嗎?當然是可以的。可以使用 -l 選項查看 killall 所支持的所有信號:
[alvin@VM_0_16_centos test]$ killall -l HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS UNUSED
你可以使用 -s 選項(后面跟一個信號名)來向一個進程發(fā)送特殊信號。
7、交互式操作
如果你在殺死多個進程時不太放心,擔心把不該殺死的進程給殺死了,那么你可以使用 -i 選項,這樣就可以自由決定哪些進程應該被殺死,哪些進程應該被保留。
[alvin@VM_0_16_centos test]$ killall -i hello* Kill hello2(13825) ? (y/N) y Kill hello3(13831) ? (y/N) N hello: no process found hello1: no process found hello3: no process found hello.c: no process found [2]- Terminated ./hello2
8、等待直到某個進程被終止
當一個信號被發(fā)送至某個進程,如果你想確定該進程已經(jīng)被殺死了才返回執(zhí)行結果,可以使用 -w 選項,其中 w 代表 wait ,如下:
[alvin@VM_0_16_centos test]$ killall -w hello1 [4]+ Terminated ./hello1
關于怎么在Linux中使用killall命令終止進程就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁名稱:怎么在Linux中使用killall命令終止進程
網(wǎng)頁路徑:http://m.2m8n56k.cn/article40/jssgeo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供建站公司、用戶體驗、軟件開發(fā)、微信公眾號、網(wǎng)站導航、移動網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)