pcount=$(ps -fe|grep cmd-test.php |grep -v grep|wc -l|awk '{print $1}')
if [ $pcount -eq ]
then
echo "running two process"
else
echo "no have two process"
fi
ps -fe|grep cmd-test.php |grep -v grep
if [ $? -eq ]
then
echo "running"
else
echo "need to start process"
fi
#!/bin/bash
pcount=$(ps -fe|grep cmd-test.php |grep -v grep|wc -l|awk '{print $1}')
if [ $pcount -eq ]
then
echo "running two process"
else
echo "process != 2,then need to restart process"
ps aux | grep "cmd-test.php" |grep -v grep| cut -c - | xargs kill -
echo "process kill over,starting the process"
nohup /usr/local/php/bin/php /data1/htdocs/paymenttest.comprame.com/bin/cmd-test.php query/execute &
nohup /usr/local/php/bin/php /data1/htdocs/paymenttest.comprame.com/bin/cmd-test.php notice/executeCycle &
fi
[CP]Linux根據程序名稱Kill多個程序
經常需要Kill多個程序,這些程序包含共同的關鍵字,可以用一條指令Kill掉它們。
ps aux | grep “cmd-test.php” |grep -v grep| cut -c 9-15 | xargs kill -9
管道符“|”用來隔開兩個指令,管道符左邊指令的輸出會作為管道符右邊指令的輸入。下面說說用管道符聯接起來的幾個指令:
“ps aux” 檢視所有程序的指令。這時檢索出的程序将作為下一條指令grep “common”的輸入。
“grep “common” 選出所有含有關鍵字”common”的程序。
“cut -c 9-15” 截取輸入行的第9個字元到第15個字元,而這正好是程序号PID。
“xargs kill –9” 中的xargs指令是用來把前面指令的輸出結果(PID)作為”kill –9”指令的參數,并執行該指令。”kill –9” 會強行殺掉指定程序。
From:http://forfuture1978.javaeye.com/blog/667309
項目執行個體:
項目中監控兩個程序,如果程序丢失,則自動啟動,另外把這個shell腳本配置在linux下面的crontab,定時執行即可!
#!/bin/bash
pcount=$(ps -fe|grep cmd-test.php |grep -v grep|wc -l|awk '{print $1}')
if [ $pcount -eq ]
then
echo "running two process"
else
echo "process != 2,then need to restart process"
ps aux | grep "cmd-test.php" |grep -v grep| cut -c - | xargs kill -
echo "process kill over,starting the process"
nohup /usr/local/php/bin/php /data1/htdocs/xxx/bin/cmd-test.php query/execute &
nohup /usr/local/php/bin/php /data1/htdocs/xxx/bin/cmd-test.php notice/executeCycle &
fi