我每分钟运行一次cron作业,这将启动bash shell脚本:
# Automatically reboot the server if it goes down
* * * * * /root/auto-restart.sh我可以手动运行脚本,但是每当我尝试通过cron作业运行它时,它就不会运行。我做了一些调试,发现这个命令是罪魁祸首:
pgrep -fcl auto-restart.sh总之,当手动运行时,该命令返回正确的值,并且只在脚本当前运行时列出auto-restart.sh (正如它应该的那样)。
但是,当cron作业启动它时,它将显示pgrep -fl auto-restart.sh的输出,如下所示(当它运行时):
3110 sh
3111 auto-restart.sh下面是我正在使用的代码:
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完整的脚本在这里:https://pastebin.com/bDyzxFXq
发布于 2018-10-12 07:50:00
当cron运行/root/auto-restart.sh时,它按照sh -c /root/auto-restart.sh的方式使用sh运行它。由于您已经在pgrep中使用了D5选项,所以pgrep在运行的进程的命令行中的任何位置都会查找auto-restart.sh;因此它匹配auto-restart.sh和sh -c /root/auto-restart.sh。后者在sh的输出中显示为普通的pgrep -l。
pgrep -c auto-restart.sh会给你你想要的行为。(我放弃了-l,因为它对-c没有意义。)
(您的服务器可能有一个看门狗定时器,这可能更合适--尽管我认为,如果服务器仍然运行得足够好,可以运行cron作业,但在其他情况下被认为处于瘫痪状态,那么看门狗就不会绊倒。)
https://unix.stackexchange.com/questions/474990
复制相似问题