每天一個Linux命令(61)killall命令


    killall命令用進程的名字來殺死進程。

 

    (1)用法:

    用法:  killall [ -egiqvw ] [ -signal ] [進程名稱]

格式:killall -<signame> <command-name> 
格式:killall -<signum> <command-name> 
    發送指定的信號到名字為<command-name>的所有進程。指定的信號可以是名稱<signame>,也可以是信號對應的數字<signum>。
下面是常用的信號:
第一列為<signame>,第二列為<signum>,第三列為信號的含義。 
HUP       1       終端斷線 
INT       2       中斷(同 Ctrl + C) 
QUIT      3       退出(同 Ctrl + \) 
KILL      9       強制終止 
TERM     15       終止 
CONT     18       繼續(與STOP相反, fg/bg命令) 
STOP     19       暫停(同 Ctrl + Z

 

    (2)功能:

    功能:  用來結束同名的的所有進程

    我們可以使用kill命令殺死指定進程PID的進程,如果要找到我們需要殺死的進程,我們還需要在之前使用ps等命令再配合grep來查找進程,而killall把這兩個過程合二為一,是一個很好用的命令。

     killall發送一條信號給所有運行任意指定命令的進程. 如果沒有指定信號名, 則發送 SIGTERM。

 

    (3)選項參數:

      1) -I         在發送信號給指定名稱的進程時忽略大小寫

      2) -i         交互模式,殺死進程前先詢問用戶

      3) -e          要求匹配進程名稱(當進程名大於15個字符時有用http://os.51cto.com/art/201405/437678.htm)

      4) -s         發送指定的信號

      5) -v         報告信號是否發送成功

      6) -w        等待所有殺的進程死去。killall 會每秒檢查一次是否任何被殺的進程仍然存在, 僅當都死光后才返回. 

               注意: 如果信號被忽略或沒有起作用, 或者進程停留在僵屍狀態, killall 可能會永久等待。

 

    (4)實例:

      1)[root@localhost sunmeng]# killall -9 bash

[root@localhost sunmeng]# ps -ef |grep bash
root        826      1  0 6月26 ?       00:00:01 /bin/bash /usr/sbin/ksmtuned
sunmeng    8914   8772  0 6月26 ?       00:00:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
sunmeng   71359  71351  0 07:10 pts/0    00:00:00 bash
root      71399  71392  0 07:10 pts/0    00:00:00 bash
root      71439  71399  0 07:10 pts/0    00:00:00 grep --color=auto bash
[root@localhost sunmeng]# killall bash      //並沒有起到任何作用
[root@localhost sunmeng]# ps -ef |grep bash
root        826      1  0 6月26 ?       00:00:01 /bin/bash /usr/sbin/ksmtuned
sunmeng    8914   8772  0 6月26 ?       00:00:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
sunmeng   71359  71351  0 07:10 pts/0    00:00:00 bash
root      71399  71392  0 07:10 pts/0    00:00:00 bash
root      71450  71399  0 07:10 pts/0    00:00:00 grep --color=auto bash

      當使用killall -9 bash時將會退出root和用戶的登錄。

      2)[root@localhost Documents]# killall -9 vi        殺死進程名為vi的所有進程

[root@localhost Documents]# vi text1.txt    

[2]+  已停止               vi text1.txt
[root@localhost Documents]# vi text2.txt

[3]+  已停止               vi text2.txt
[root@localhost Documents]# ps -ef | grep "vi"
root        784      1  0 6月26 ?       00:00:00 /usr/bin/abrt-watch-log -F BUG: WARNING: at WARNING: CPU: INFO: possible recursive locking detected ernel BUG at list_del corruption list_add corruption do_IRQ: stack overflow: ear stack overflow (cur: eneral protection fault nable to handle kernel ouble fault: RTNL: assertion failed eek! page_mapcount(page) went negative! adness at NETDEV WATCHDOG ysctl table check failed : nobody cared IRQ handler type mismatch Machine Check Exception: Machine check events logged divide error: bounds: coprocessor segment overrun: invalid TSS: segment not present: invalid opcode: alignment check: stack segment: fpu exception: simd exception: iret exception: /var/log/messages -- /usr/bin/abrt-dump-oops -xtD
root       1313      1  0 6月26 ?       00:00:00 /usr/sbin/libvirtd
sunmeng    9044      1  0 6月26 ?       00:00:04 /usr/libexec/goa-identity-service
sunmeng    9080      1  0 6月26 ?       00:00:00 /usr/libexec/dconf-service
root      71852  71399  0 07:21 pts/0    00:00:00 vi text1.txt
root      71856  71399  0 07:21 pts/0    00:00:00 vi text2.txt
root      71861  71399  0 07:21 pts/0    00:00:00 grep --color=auto vi
[root@localhost Documents]# ps -ef |grep "text"      //根據text能找着名字為vi的進程
root      71852  71399  0 07:21 pts/0    00:00:00 vi text1.txt
root      71856  71399  0 07:21 pts/0    00:00:00 vi text2.txt
root      71868  71399  0 07:22 pts/0    00:00:00 grep --color=auto text
[root@localhost Documents]# killall text        //但並不能通過它來刪除進程     
text: no process found
[root@localhost Documents]# killall vi        //還是不起效果
[root@localhost Documents]# ps -ef | grep "vi"
root        784      1  0 6月26 ?       00:00:00 /usr/bin/abrt-watch-log -F BUG: WARNING: at WARNING: CPU: INFO: possible recursive locking detected ernel BUG at list_del corruption list_add corruption do_IRQ: stack overflow: ear stack overflow (cur: eneral protection fault nable to handle kernel ouble fault: RTNL: assertion failed eek! page_mapcount(page) went negative! adness at NETDEV WATCHDOG ysctl table check failed : nobody cared IRQ handler type mismatch Machine Check Exception: Machine check events logged divide error: bounds: coprocessor segment overrun: invalid TSS: segment not present: invalid opcode: alignment check: stack segment: fpu exception: simd exception: iret exception: /var/log/messages -- /usr/bin/abrt-dump-oops -xtD
root       1313      1  0 6月26 ?       00:00:00 /usr/sbin/libvirtd
sunmeng    9044      1  0 6月26 ?       00:00:04 /usr/libexec/goa-identity-service
sunmeng    9080      1  0 6月26 ?       00:00:00 /usr/libexec/dconf-service
root      71852  71399  0 07:21 pts/0    00:00:00 vi text1.txt
root      71856  71399  0 07:21 pts/0    00:00:00 vi text2.txt
root      71920  71399  0 07:23 pts/0    00:00:00 grep --color=auto vi
[root@localhost Documents]# killall -9 vi      //經過測試,只有加上-9才會起作用
[2]-  已殺死               vi text1.txt
[3]+  已殺死               vi text2.txt

      3)[root@localhost Documents]# killall -l      打印所有已知信號列表

[root@localhost Documents]# 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
[root@localhost Documents]# kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT          4) SIGILL       5) SIGTRAP
 6) SIGABRT       7) SIGBUS       8) SIGFPE          9) SIGKILL      10) SIGUSR1
11) SIGSEGV       12) SIGUSR2      13) SIGPIPE        14) SIGALRM      15) SIGTERM
16) SIGSTKFLT     17) SIGCHLD      18) SIGCONT        19) SIGSTOP      20) SIGTSTP
21) SIGTTIN       22) SIGTTOU       23) SIGURG         24) SIGXCPU      25) SIGXFSZ
26) SIGVTALRM     27) SIGPROF       28) SIGWINCH       29) SIGIO        30) SIGPWR
31) SIGSYS        34) SIGRTMIN      35) SIGRTMIN+1     36) SIGRTMIN+2   37) SIGRTMIN+3
38) SIGRTMIN+4    39) SIGRTMIN+5    40) SIGRTMIN+6     41) SIGRTMIN+7   42) SIGRTMIN+8
43) SIGRTMIN+9    44) SIGRTMIN+10   45) SIGRTMIN+11    46) SIGRTMIN+12  47) SIGRTMIN+13
48) SIGRTMIN+14   49) SIGRTMIN+15   50) SIGRTMAX-14    51) SIGRTMAX-13  52) SIGRTMAX-12
53) SIGRTMAX-11   54) SIGRTMAX-10   55) SIGRTMAX-9     56) SIGRTMAX-8   57) SIGRTMAX-7
58) SIGRTMAX-6    59) SIGRTMAX-5    60) SIGRTMAX-4     61) SIGRTMAX-3   62) SIGRTMAX-2
63) SIGRTMAX-1    64) SIGRTMAX    

      4)[root@localhost Documents]# killall -s TERM vi      發送指定的信號給指定名稱的進程

[root@localhost Documents]# killall -s TERM vi
[root@localhost Documents]# ps -ef | grep vi
root        784      1  0 6月26 ?       00:00:00 /usr/bin/abrt-watch-log -F BUG: WARNING: at WARNING: CPU: INFO: possible recursive locking detected ernel BUG at list_del corruption list_add corruption do_IRQ: stack overflow: ear stack overflow (cur: eneral protection fault nable to handle kernel ouble fault: RTNL: assertion failed eek! page_mapcount(page) went negative! adness at NETDEV WATCHDOG ysctl table check failed : nobody cared IRQ handler type mismatch Machine Check Exception: Machine check events logged divide error: bounds: coprocessor segment overrun: invalid TSS: segment not present: invalid opcode: alignment check: stack segment: fpu exception: simd exception: iret exception: /var/log/messages -- /usr/bin/abrt-dump-oops -xtD
root       1313      1  0 6月26 ?       00:00:00 /usr/sbin/libvirtd
sunmeng    9044      1  0 6月26 ?       00:00:04 /usr/libexec/goa-identity-service
sunmeng    9080      1  0 6月26 ?       00:00:00 /usr/libexec/dconf-service
root      72199  71399  0 07:27 pts/0    00:00:00 vi text1.txt
root      72768  71399  0 07:49 pts/0    00:00:00 vi text1.txt
root      72773  71399  0 07:49 pts/0    00:00:00 vi text2.txt
root      72902  71399  0 07:52 pts/0    00:00:00 grep --color=auto vi
[root@localhost Documents]# killall TERM vi
TERM: no process found
[root@localhost Documents]# killall -s KILL vi
[3]-  已殺死               vi text1.txt
[4]+  已殺死               vi text2.txt
[2]+  已殺死               vi text1.txt

      5)[root@localhost Documents]# killall -w -s 9 ping      發送信號給指定進程,等待它被殺死

[root@localhost Documents]# ps -ef | grep "ping"
root      73631  71399  0 08:13 pts/0    00:00:00 ping 192.168.0.18
root      73649  71399  0 08:13 pts/0    00:00:00 ping -b 192.168.0.18
root      73703  71399  0 08:16 pts/0    00:00:00 grep --color=auto ping
[root@localhost Documents]# killall -w -s -9 ping
-9: 未知的信號; killall -l 顯示信號列表。
[root@localhost Documents]# killall -w -s 9 ping
[6]-  已殺死               ping 192.168.0.18
[7]+  已殺死               ping -b 192.168.0.18
[root@localhost Documents]# ps -ef | grep "ping"
root      73736  71399  0 08:16 pts/0    00:00:00 grep --color=auto ping

      6)[root@localhost Documents]# killall -help

[root@localhost Documents]# killall -help
Usage: killall [-Z 上下文] [-u 用戶名] [ -eIgiqrvw ] [ -信號 ] 程序名...
       killall -l, --list
       killall -V, --version

  -e,--exact          require exact match for very long names
  -I,--ignore-case    case insensitive process name match
  -g,--process-group  kill process group instead of process
  -y,--younger-than   kill processes younger than TIME
  -o,--older-than     kill processes older than TIME
  -i,--interactive    ask for confirmation before killing
  -l,--list           list all known signal names
  -q,--quiet          don't print complaints
  -r,--regexp         interpret NAME as an extended regular expression
  -s,--signal SIGNAL  send this signal instead of SIGTERM     //
  -u,--user USER      kill only process(es) running as USER
  -v,--verbose        report if the signal was successfully sent
  -V,--version        display version information
  -w,--wait           wait for processes to die
  -Z,--context 正則表達式 僅殺死含有指定上下文的進程
                          (必須在其他參數前使用)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM