转:http://lionbule.iteye.com/blog/713585
kill -9 `pidof 进程名`
[root@web_test test]# ps -ef|grep nginx
root 1155 1 0 Mar16 ? 00:00:00 nginx: master process nginx
www 1157 1155 0 Mar16 ? 00:00:04 nginx: worker process
www 1158 1155 0 Mar16 ? 00:00:02 nginx: worker process
www 1159 1155 0 Mar16 ? 00:00:04 nginx: worker process
www 1160 1155 0 Mar16 ? 00:00:05 nginx: worker process
root 19451 19433 0 17:17 pts/4 00:00:00 grep nginx
[root@web_test test]# pidof nginx
1160 1159 1158 1157 1155
root 1155 1 0 Mar16 ? 00:00:00 nginx: master process nginx
www 1157 1155 0 Mar16 ? 00:00:04 nginx: worker process
www 1158 1155 0 Mar16 ? 00:00:02 nginx: worker process
www 1159 1155 0 Mar16 ? 00:00:04 nginx: worker process
www 1160 1155 0 Mar16 ? 00:00:05 nginx: worker process
root 19451 19433 0 17:17 pts/4 00:00:00 grep nginx
[root@web_test test]# pidof nginx
1160 1159 1158 1157 1155
[root@web_test test]# pstree -p 1155
nginx(1155)─┬─nginx(1157)
├─nginx(1158)
├─nginx(1159)
└─nginx(1160)
如果想在脚本里只获取PID,那么可以用如下脚本。目前收集两种方法:
方法一
$ps x|grep xxx |awk '{print $1}'
e.g.
ps x|grep java |awk '{print $1}'
注释:
1、xxx为执行的命令名称
2、举个例子,获取当前用户下的java进程 【pid】
[admin@vm168a ~]$ ps x|grep java |awk '{print $1}'
16920
3、用到三个命令,ps、grep、awk。
方法二(推荐)
$pgrep xxx
e.g.
pgrep java