Linux kill 殺死指定程序
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
一 殺死指定程序
現知道有一個curl執行緒正在執行,需要殺死
[email protected]$ curl -y 30 -Y 1 -m 300 -x 8.8.8.8:808 -o html_baidu http://www.baidu.com
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:21 --:--:-- 0
ps -ef 查詢執行程序
[email protected]$ ps -ef | grep curl
yanggang 10992 25473 0 14:11 pts/0 00:00:00 curl -y 30 -Y 1 -m 300 -x 8.8.8.8:808 -o html_baidu http://www.baidu.com
yanggang 18591 11235 0 14:11 pts/1 00:00:00 grep --color=auto curl
ps -ef 查詢並過濾程序id:
yanggang 9201 25473 0 14:13 pts/0 00:00:00 curl -y 30 -Y 1 -m 300 -x 8.8.8.8:808 -o html_baidu http://www.baidu.com
yanggang 13612 11235 0 14:13 pts/1 00:00:00 grep --color=auto curl
[email protected]$ ps -ef | grep curl | grep -v grep | cut -c 15-20
25473
ps -ef 查詢並過濾程序id,並殺死該程序:
[email protected]$ ps -ef | grep curl
yanggang 13390 28367 0 14:15 pts/3 00:00:00 curl -y 30 -Y 1 -m 300 -x 8.8.8.8:808 -o html_baidu http://www.baidu.com (殺死程序前)
yanggang 16946 11235 0 14:15 pts/1 00:00:00 grep --color=auto curl
[email protected]$ ps -ef | grep curl | grep -v grep | cut -c 15-20
28367
[email protected]$ ps -ef | grep curl | grep -v grep | cut -c 15-20 | xargs kill -9
[email protected]$ ps -ef | grep curl
yanggang 13072 11235 0 14:16 pts/1 00:00:00 grep --color=auto curl (殺死程序後,無此程序)
或者:
kill -9 `ps -ef|grep “processname” | grep -v "grep"|awk '{print $2} '`
二 殺死批量程序
for pid in $(ps -ef | grep curl | grep -v grep | cut -c 15-20); do (獲取程序id陣列,並迴圈殺死所有程序)
echo $pid
kill -9 $pid
done
貼出原始碼:
# !/bin/shfor pid in $(ps -ef | grep curl | grep -v grep | cut -c 15-20); do echo $pid kill -9 $piddone#while [ ! -z $(ps -ef | grep curl | grep -v grep | cut -c 9-15) ]#do# ps -ef | grep curl | grep -v grep | cut -c 15-20 | xargs kill -9# ps -ef | grep curl | grep -v grep | cut -c 9-15 | xargs kill -9#done
推薦參考: