我每分钟运行一个 cron 作业,它启动一个 bash shell 脚本:
# Automatically reboot the server if it goes down
* * * * * /root/auto-restart.sh
Run Code Online (Sandbox Code Playgroud)
我可以手动运行该脚本,但每当我尝试通过 cron 作业运行它时,它都不会运行。我调试了一下,发现这个命令是罪魁祸首:
pgrep -fcl auto-restart.sh
Run Code Online (Sandbox Code Playgroud)
总而言之,当手动运行时,该命令返回正确的值,并且仅在脚本当前正在运行时列出 auto-restart.sh(正如它应该的那样)。
但是当 cron 作业启动它时,它会显示如下输出pgrep -fl auto-restart.sh
(当它运行时):
3110 sh
3111 auto-restart.sh
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码:
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] …
Run Code Online (Sandbox Code Playgroud)