Linux 下使用 killall 命令終止進程的 8 大用法

Linux 的命令行提供不少命令來殺死進程。好比,你能夠向 kill 命傳遞一個PID來殺死進程;pkill 命令使用一個正則表達式做爲輸入,因此和該模式匹配的進程都被殺死。
正則表達式

可是還有一個命令叫 killall ,默認狀況下,它精確地匹配參數名,而後殺死匹配進程。在這篇文章中,咱們將討論有關這個命令的實際應用。centos

默認狀況下,killall 命令將向一個/組進程發送一個 SIGTERM 信號,可是,也能夠經過參數發送一個指定的信號。bash

下面咱們經過例子詳細介紹 killall 的 8 大用法。微信

1. 基本用法

假如咱們 3 個進程在運行,分別是 hello1, hello2, hello3 ,如今咱們想殺死 hello1 進程,能夠直接使用以下方式:ssh

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 進程已經被殺死了。ui

剩下的 hello2 和 hello3 進程,咱們想一次性殺死他們,也就是批量殺死進程,能夠以下操做:spa

[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. 終止某個用戶所運行的進程

咱們能夠殺死以知足某個正則表達式的一組進程,一樣的,咱們也能夠殺死某個用戶運行的全部進程。code

好比,用戶 harry 如今運行以下幾個進程:

[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複製代碼

咱們如今想殺死 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. 終於時間的方式終止進程

假如咱們如今運行了不少程序,咱們只想殺死運行時間超過 5h 的進程,那麼可使用 -o 選項,其中 o 表明 older 以下:

killall -o 5h複製代碼

一樣地,若是你想殺死進行時間小於 4h 的進程,那麼可使用 -y 選項,其中 y 表明 younger ,以下:

killall -y 4h複製代碼

這兩個選項一樣很是粗暴,也會把終端退出,因此先不演示了。

4. 忽略大小寫

默認狀況下,killall 命令是大小寫敏感的,因此咱們若是寫錯大小寫,將沒法正確殺死進程。

[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. 關閉命令執行回顯

默認狀況下,killall 會告訴你命令執行狀況,可是,咱們若是不關心它的執行結果,只想讓它靜默執行,該怎麼辦?只需加上 -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 命令將發送 SIGTERM 信號,那麼,安能夠發送其它信號嗎?固然是能夠的。可使用 -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 選項(後面跟一個信號名)來向一個進程發送特殊信號。

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. 等待直到某個進程被終止

當一個信號被髮送至某個進程,若是你想肯定該進程已經被殺死了才返回執行結果,可使用 -w 選項,其中 w 表明 wait ,以下:

[alvin@VM_0_16_centos test]$ killall -w hello1
[4]+  Terminated              ./hello1複製代碼

這裏好像看不出什麼效果,但實際執行的時候,能夠發現執行結果會在一兩秒後出現,而不加 -w 選項的話,執行結果立刻就顯示。

碼字不易,若是您以爲有幫助,麻煩點個贊再走唄~

-----------------

微信搜索公衆號【良許Linux】,後臺回覆關鍵字:

  1. 回覆【1024】:免費獲取不斷更新的海量技術資源;

  2. 回覆【CSDN】:免費進入CSDN資源下載羣;

  3. 回覆【進羣】:免費進入高手如雲技術交流羣。

乾貨分享 · 資源共享 · 認知提升 · 英語學習

碼字不易,若是您以爲有幫助,麻煩點個贊再走唄~

-----------------

我是良許,世界500強外企 Linux 開發工程師,專業生產 Linux 乾貨。歡迎關注個人公衆號「良許Linux」,回覆「1024」獲取最新最全的技術資料,回覆「入羣」進入高手如雲技術交流羣。

相關文章
相關標籤/搜索