每天一个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 正则表达式 仅杀死含有指定上下文的进程 (必须在其他参数前使用)