#!/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
問題搞定!