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