问题
Linux cron配置定时任务启动脚本,如果运行时间覆盖cron下次的时间,需要做进程检测,否则会有多个脚本运行。
解决方法
shell脚本运行时创建pid.file
#!/bin/bash
set -e
PIDFILE=/var/run/test.pid
if [ -f $PIDFILE ]
then
PID=$(cat $PIDFILE)
ps -p $PID > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "Process already running"
exit 1
else
## Process not found assume not running
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "Could not create PID file2"
exit 1
fi
fi
else
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "Could not create PID file"
exit 1
fi
fi
echo "sleep 10"
sleep 10
echo "sleep 10 done"
rm $PIDFILE