[Bash]kill指定的进程名
生活随笔
收集整理的這篇文章主要介紹了
[Bash]kill指定的进程名
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過bash來kill指定的進程名,bash文件名為:/home/zcm/bin/d.sh,內容如下:
#!/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
運行結果如圖:
?
事實上,上面的腳本沒有kill掉指定的進程,只是將要kill的進程信息列了出來。我們只要加上“kill進程的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?
現在的運行結果是:
[zcm@bin #117]$sh b.sh bash the pid is 2405 the pid is 2649但是我發現:kill終端本身好像不起作用,但是當我開了幾個firefox后再測試,發現所有打開的firefox進程都被kill掉了。實驗證明,這個腳本是成功的!
總結
以上是生活随笔為你收集整理的[Bash]kill指定的进程名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows系统安装运行库
- 下一篇: Solr 配置文件之schema.xml