下面就是在shell script里面判断某个程序是否还在运行:
export PROCESS_PID=/****/****.pid ---------------------------在某一个目录下定义一个文件用来存储进程号
export LOG_FILE=/****/***.log -------------定义一个log 文件
#=============================================
# Ensure that the service is not running
#=============================================
CURR_PID=$$
CURR_PROCESS_NAME=`ps -f -p $CURR_PID|grep $USER_ID|awk '{print $8, " ", $9}'`
# Read the process ID from the file
grep -i 'PROC PID' $PROCESS_PID 2>/dev/null |cut -f2 -d"=" | read pid
# Check if process is still running
if [ "$pid" ]; then
ps -f -p $pid|grep $USER_ID|grep "`echo $CURR_PROCESS_NAME`" > /dev/null 2>&1
if [[ $? = 0 ]]; then
echo "Warning: Existing process $pid found. Aborting current process... " >> $LOG_FILE
exit
fi
fi
(echo "PROC PID = "$CURR_PID > $PROCESS_PID) >> $LOG_FILE 2>&1 ---如果改进程没有运行则将之写入文件
if [[ $? = 0 ]]
then
echo "Process $CURR_PID started!!/n" >> $LOG_FILE
fi
这样可以防止一个程序可能多个运行的情况发生。