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