[Bash]kill指定的进程名

来源:互联网 发布:电脑定时提醒软件 编辑:程序博客网 时间:2024/06/14 14:47

通过bash来kill指定的进程名,bash文件名为:/home/zcm/bin/d.sh,内容如下:

#!/bin/shif [ "$1" = "" ]; thenecho "Usage: sh $0 <processname>"exit 0fi#s1=`ps -ef|grep $1|grep -v grep|awk '{print $2}'`s1=`ps -ef|grep $1|grep -v "sh $1"`echo "$s1"echo "----------------------------"echo "$s1"|while read linedoif [ "$line" != "" ]; thenecho "$line"#echo "the pid is $line"#kill "$line"elseecho "the pid is NULL"fidone


运行结果如图:

点击看大图

 

事实上,上面的脚本没有kill掉指定的进程,只是将要kill的进程信息列了出来。我们只要加上“kill进程的pid”就可以了,完整代码如下:

#!/bin/shif [ "$1" = "" ]; thenecho "Usage: sh $0 <processname>"exit 0fis1=`ps -ef|grep $1|grep -v "sh $1"|awk '{print $2}'`echo "$s1"|while read linedoif [ "$line" != "" ]; thenecho "the pid is $line"kill $lineelseecho "the pid is NULL"fidone

 

现在的运行结果是:

[zcm@bin #117]$sh b.sh bashthe pid is 2405the pid is 2649

但是我发现:kill终端本身好像不起作用,但是当我开了几个firefox后再测试,发现所有打开的firefox进程都被kill掉了。实验证明,这个脚本是成功的!

原创粉丝点击