[Bash]kill指定的进程名
生活随笔
收集整理的這篇文章主要介紹了
[Bash]kill指定的进程名
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
通過(guò)bash來(lái)kill指定的進(jìn)程名,bash文件名為:/home/zcm/bin/d.sh,內(nèi)容如下:
#!/bin/sh if [ "$1" = "" ]; thenecho "Usage: sh $0 <processname>"exit 0 fi #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 line doif [ "$line" != "" ]; thenecho "$line" # echo "the pid is $line" # kill "$line"elseecho "the pid is NULL"fi done
運(yùn)行結(jié)果如圖:
?
事實(shí)上,上面的腳本沒(méi)有kill掉指定的進(jìn)程,只是將要kill的進(jìn)程信息列了出來(lái)。我們只要加上“kill進(jìn)程的pid”就可以了,完整代碼如下:
#!/bin/sh if [ "$1" = "" ]; thenecho "Usage: sh $0 <processname>"exit 0 fi s1=`ps -ef|grep $1|grep -v "sh $1"|awk '{print $2}'` echo "$s1"|while read line doif [ "$line" != "" ]; thenecho "the pid is $line"kill $lineelseecho "the pid is NULL"fi done?
現(xiàn)在的運(yùn)行結(jié)果是:
[zcm@bin #117]$sh b.sh bash the pid is 2405 the pid is 2649但是我發(fā)現(xiàn):kill終端本身好像不起作用,但是當(dāng)我開(kāi)了幾個(gè)firefox后再測(cè)試,發(fā)現(xiàn)所有打開(kāi)的firefox進(jìn)程都被kill掉了。實(shí)驗(yàn)證明,這個(gè)腳本是成功的!
總結(jié)
以上是生活随笔為你收集整理的[Bash]kill指定的进程名的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Windows系统安装运行库
- 下一篇: Solr 配置文件之schema.xml