pcount=$(ps -fe|grep cmd-test.php |grep -v grep|wc -l|awk '{print $1}')
if [ $pcount -eq 2 ]
then
echo "running two process"
else
echo "no have two process"
fi
ps -fe|grep cmd-test.php |grep -v grep
if [ $? -eq 0 ]
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 2 ]
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 9-15 | xargs kill -9
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 2 ]
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 9-15 | xargs kill -9
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