kill命令
kill命令
1. kill
作用:根据进程号杀死进程
用法: kill 进程ID,即kill pid号(杀死进程最安全的方法)
注:pid号可以通过ps -aux或者ps -ef获取(确定要杀死进程的PID或PPID(父进程))
ps -ef | grep httpd 或 ps -aux | grep httpd
root@fcola:/#ps -ef | grep sendmail smmsp 14354 14337 0 00:08 pts/1 00:00:00 sendmail start smmsp 14356 3154 0 00:08 pts/0 00:00:00 sendmail -s smmsp 14359 3154 0 00:09 pts/0 00:00:00 sendmail start smmsp 14360 14337 0 00:09 pts/1 00:00:00 sendmail state root17723 15753 0 01:08 pts/2 00:00:00 vim sendmail.mc root 19022 1 0 01:26 ? 00:00:03 gedit /etc/mail/sendmail.mc root 20674 15753 0 01:55 pts/2 00:00:00 grep sendmail root@loris-desktop:/#kill 20674 root@loris-desktop:/#kill -9 14354 注意:kill -9 来强制终止退出“grep java”的输出结果是,所有含有关键字“java”的进程。
“grep -v grep”是在列出的进程中去除含有关键字“grep”的进程。
“cut -c 9-15”是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。
“xargs kill -s 9”中的xargs命令是用来把前面命令的输出结果(PID)作为“kill -s 9”命令的参数,并执行该命令。“kill -s 9”会强行杀掉指定进程。
难道你不想抱怨点什么?没错太长了
$ ps -ef | grepjava | awk '{print $2}' | xargs kill -9
其中awk '{print $2}' 的作用就是打印(print)出第二列的内容。根据常规篇,可以知道ps输出的第二列正好是PID。就把进程相应的PID通过xargs传递给kill作参数,杀掉对应的进程。
$kill -s 9 `ps -aux | grepjava | awk '{print $2}'`
2. killall 作用:通过程序的名字,直接杀死所有进程 用法:killall 正在运行的程序名 killall命令杀死同一进程组内的所有进程。其允许指定要终止的进程的名称,而非PID。 举例: root@fcola:/#ps -ef | grep sendmail smmsp 14354 14337 0 00:08 pts/1 00:00:00 sendmail start smmsp 14356 3154 0 00:08 pts/0 00:00:00 sendmail -s smmsp 14359 3154 0 00:09 pts/0 00:00:00 sendmail start smmsp 14360 14337 0 00:09 pts/1 00:00:00 sendmail state root 17723 15753 0 01:08 pts/2 00:00:00 vim sendmail.mc root 19022 1 0 01:26 ? 00:00:03 gedit /etc/mail/sendmail.mc root 20683 15753 0 01:59 pts/2 00:00:00 grep sendmail root@fcola:/#killall -9 sendmail root@fcola:/# ps -ef | grep sendmail root 17723 15753 0 01:08 pts/2 00:00:00 vim sendmail.mc root 19022 1 0 01:26 ? 00:00:03 gedit /etc/mail/sendmail.mc root 20686 15753 0 01:59 pts/2 00:00:00 grep sendmail root@fcola:/#killall -9 sendmail 注意:该命令可以使用 -9 参数来强制杀死进程 3. pkill 作用:通过程序的名字,直接杀死所有进程 用法:#pkill 正在运行的程序名 也可以用 $pkill -9 java 举例: root@fcola:/# ps -ef | grep sendmail root 17723 15753 0 01:08 pts/2 00:00:00 vim sendmail.mc root 19022 1 0 01:26 ? 00:00:03 gedit /etc/mail/sendmail.mc root 21480 15753 0 02:03 pts/2 00:00:00 grep sendmail root@fcola:/#pkill sendmail首先使用who命令查看在线用户,然后踢人。
强制踢人命令格式:pkill -kill -t tty
解释:
pkill -kill -t 踢人命令
tty 所踢用户的TTY或者pts/x(x代表数字),代表登录终端
如上踢出liu用户的命令为: pkill -kill -t pts/1
只有root用户才能踢人。
如果同时有二个人用root用户登录,任何其中一个可以踢掉另一个。
任何用户都可以踢掉自己-_-。