#!/bin/sh
WORKDIR=/home/guoq/myprogram/Debug
PROGRAM=program
LOGFILE=/var/log/monitor.log
eval "PID=`ps -ef|grep ${PROGRAM} | grep -v grep | awk '{printf $2}'`"
eval echo "PID is ${PID}" >> ${LOGFILE}
if [ x"${PID}" = x"" ]; then
eval echo "`date`: program is down, so restart it!" >> ${LOGFILE}
eval cd ${WORKDIR}
eval nohup ${PROGRAM} &
else
eval echo "`date`: ${PID} still running" >> ${LOGFILE}
fi
上述脚本,手动执行与放在crontab中自动执行结果不同;
最后发现,如果自动执行,那么其他程序的路径中也会出现program,
awk '{printf $2}'的时候,会同时得到几行的内容串接在一起,始终不为空;
将
PROGRAM=program
改为
PROGRAM=./program
问题搞定!