Linux判断进程是否存在并启动该进程


 

#!/bin/bash
#判断进程是否存在,如果不存在就启动它
PIDS=`ps -ef |grep myprocess |grep -v grep | awk '{print $2}'`
if [ "$PIDS" != "" ]; then
echo "myprocess is runing!"
else
cd /root/
./myprocess
#运行进程
fi

 grep -v grep命令:去除包含grep的进程行 ,避免影响最终数据的正确性 。

[root@CENTOS57 eq]# ps -ef |grep led
root       9240   1562  0 Oct22 pts/0    00:01:09 ./ledThread ledall
root 9809   1562  0 06:41 pts/0    00:00:00 grep --color=auto led

[root@CENTOS57 eq]# ps
-ef |grep led |grep -v grep root 9240 1562 0 Oct22 pts/0 00:01:09 ./ledThread ledall

[root@CENTOS57 eq]# ps -ef |grep led |grep -v grep | awk '{print $2}'
9240

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM