bash - PID checker will not work when running through crontab -
okay, have script contains infinite loop checks postgres database. works fine. in order ensure process continue run using cron continue execute file every minuet. stop multiple instances of wrote pid check script.
scriptname=`basename $0` pidfile="/var/run/${scriptname}.pid" if [ -e ${pidfile} ]; pid=`cat ${pidfile}`; echo "found pid ${pid}" running=`ps -p ${pid} -o pid=` if [ ${running} -eq ${pid} ]; runningname=`ps -p ${pid} -o command=` if [ `echo "${runningname}" | grep -c *${scriptname}*` -eq "1" ]; echo "${scriptname} running." exit 1 else echo "wrong pid file." fi else echo "outdated pid file." fi else echo "no pid file." fi echo $$ > ${pidfile}
when run process ./script.sh
not let me run instance in different command line. issue when initialize in cron create multiple instances seemingly without regard pid check. appreciated.
by removing section of code
if [ echo "${runningname}" | grep -c *${scriptname}* -eq "1" ]; then
echo "${scriptname} running." exit 1 fi
and replacing simple exit 1
able work.
for reason, cron not part of script. when running manually, keep in eventuality script script running under stored pid. small chance , happen due user error, building possibility of idea.
i still unsure why breaks in cron, does.
Comments
Post a Comment