#!/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