linux常用命令:killall 命令

  killall命令用进程的名字来杀死进程。

1.命令格式:
    killall [ -egiqvw ] [ -signal ] [进程名称]

格式:killall -<signame> <command-name>
格式:killall -<signum> <command-name>
    发送指定的信号到名字为<command-name>的所有进程。指定的信号可以是名称<signame>,也可以是信号对应的数字<signum>。
下面是常用的信号:
第一列为<signame>,第二列为<signum>,第三列为信号的含义。
HUP              终端断线
INT              中断(同 Ctrl + C)
QUIT             退出(同 Ctrl + \)
KILL             强制终止
TERM            终止
CONT            继续(与STOP相反, fg/bg命令)
STOP            暂停(同 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 xiong]# killall -9 bash

[root@localhost xiong]# ps -ef |grep bash
root                 6月26 ?       :: /bin/bash /usr/sbin/ksmtuned
xiong          6月26 ?       :: /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
xiong        : pts/    :: bash
root           : pts/    :: bash
root           : pts/    :: grep --color=auto bash
[root@localhost xiong]# killall bash      //并没有起到任何作用
[root@localhost xiong]# ps -ef |grep bash
root                 6月26 ?       :: /bin/bash /usr/sbin/ksmtuned
xiong          6月26 ?       :: /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
xiong        : pts/    :: bash
root           : pts/    :: bash
root           : pts/    :: grep --color=auto bash

说明:当使用killall -9 bash时将会退出root和用户的登录。

2)[root@localhost Documents]# killall -9 vi  杀死进程名为vi的所有进程

[root@localhost Documents]# vi text1.txt    

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

[]+  已停止               vi text2.txt
[root@localhost Documents]# ps -ef | grep "vi"
root                 6月26 ?       :: /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                6月26 ?       :: /usr/sbin/libvirtd
xiong             6月26 ?       :: /usr/libexec/goa-identity-service
xiong             6月26 ?       :: /usr/libexec/dconf-service
root           : pts/    :: vi text1.txt
root           : pts/    :: vi text2.txt
root           : pts/    :: grep --color=auto vi
[root@localhost Documents]# ps -ef |grep "text"      //根据text能找着名字为vi的进程
root           : pts/    :: vi text1.txt
root           : pts/    :: vi text2.txt
root           : pts/    :: 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                 6月26 ?       :: /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                6月26 ?       :: /usr/sbin/libvirtd
xiong             6月26 ?       :: /usr/libexec/goa-identity-service
xiong             6月26 ?       :: /usr/libexec/dconf-service
root           : pts/    :: vi text1.txt
root           : pts/    :: vi text2.txt
root           : pts/    :: grep --color=auto vi
[root@localhost Documents]#  vi      //经过测试,只有加上-9才会起作用
[]-  已杀死               vi text1.txt
[]+  已杀死               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
 ) SIGHUP       ) SIGINT       ) SIGQUIT          ) SIGILL       ) SIGTRAP
 ) SIGABRT       ) SIGBUS       ) SIGFPE          ) SIGKILL      ) SIGUSR1
) SIGSEGV       ) SIGUSR2      ) SIGPIPE        ) SIGALRM      ) SIGTERM
) SIGSTKFLT     ) SIGCHLD      ) SIGCONT        ) SIGSTOP      ) SIGTSTP
) SIGTTIN       ) SIGTTOU       ) SIGURG         ) SIGXCPU      ) SIGXFSZ
) SIGVTALRM     ) SIGPROF       ) SIGWINCH       ) SIGIO        ) SIGPWR
) SIGSYS        ) SIGRTMIN      ) SIGRTMIN+     ) SIGRTMIN+   ) SIGRTMIN+
) SIGRTMIN+    ) SIGRTMIN+    ) SIGRTMIN+     ) SIGRTMIN+   ) SIGRTMIN+
) SIGRTMIN+    ) SIGRTMIN+   ) SIGRTMIN+    ) SIGRTMIN+  ) SIGRTMIN+
) SIGRTMIN+   ) SIGRTMIN+   ) SIGRTMAX-    ) SIGRTMAX-  ) SIGRTMAX-
) SIGRTMAX-   ) SIGRTMAX-   ) SIGRTMAX-     ) SIGRTMAX-   ) SIGRTMAX-
) SIGRTMAX-    ) SIGRTMAX-    ) SIGRTMAX-     ) SIGRTMAX-   ) SIGRTMAX-
) SIGRTMAX-    ) SIGRTMAX

4)[root@localhost Documents]# killall -s TERM vi  发送指定的信号给指定名称的进程

[root@localhost Documents]# killall -s TERM vi
[root@localhost Documents]# ps -ef | grep vi
root                 6月26 ?       :: /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                6月26 ?       :: /usr/sbin/libvirtd
xiong             6月26 ?       :: /usr/libexec/goa-identity-service
xiong             6月26 ?       :: /usr/libexec/dconf-service
root           : pts/    :: vi text1.txt
root           : pts/    :: vi text1.txt
root           : pts/    :: vi text2.txt
root           : pts/    :: grep --color=auto vi
[root@localhost Documents]# killall TERM vi
TERM: no process found
[root@localhost Documents]# killall -s KILL vi
[]-  已杀死               vi text1.txt
[]+  已杀死               vi text2.txt
[]+  已杀死               vi text1.txt

5)[root@localhost Documents]# killall -w -s 9 ping  发送信号给指定进程,等待它被杀死

[root@localhost Documents]# ps -ef | grep "ping"
root           : pts/    :: ping 192.168.0.18
root           : pts/    :: ping -b 192.168.0.18
root           : pts/    :: grep --color=auto ping
[root@localhost Documents]#  ping
-: 未知的信号; killall -l 显示信号列表。
[root@localhost Documents]#  ping
[]-  已杀死               ping 192.168.0.18
[]+  已杀死               ping -b 192.168.0.18
[root@localhost Documents]# ps -ef | grep "ping"
root           : pts/    :: 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 正则表达式 仅杀死含有指定上下文的进程
                          (必须在其他参数前使用)
上一篇:java和javaScript创建方式


下一篇:[BZOJ2257][Jsoi2009]瓶子和燃料(数学)