I'm running a cron job every minute, which launches a bash shell script:
# Automatically reboot the server if it goes down
* * * * * /root/auto-restart.sh
I can manually run the script, but whenever I try running it through a cron job it won't run. I've debugged a bit and found this command to be the culprit:
pgrep -fcl auto-restart.sh
To summarize, when run manually, that command returns the correct value and only lists auto-restart.sh if the script's currently running (as it should).
But when the cron job launches it, it shows the output of pgrep -fl auto-restart.sh
as follows (when it's running):
3110 sh
3111 auto-restart.sh
Here's the bit of code I'm working with:
scriptcheck=`pgrep -fcl auto-restart.sh`
if [ $scriptcheck -gt 1 ]; then
echo "auto-restart.sh script already in use. Terminating.."
#cron job debugging..
echo "[DEBUG] scriptcheck returning greater than 1" > /tmp/debug.output
echo "[DEBUG] Value of scriptcheck: ${scriptcheck}" >> /tmp/debug.output
echo "[DEBUG] contents:" >> /tmp/debug.output
pgrep -fl auto-restart.sh >> /tmp/debug.output
exit
fi
Complete script here: https://pastebin.com/bDyzxFXq