joh*_*doe 3 cron shell-script pgrep
我每分钟运行一个 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] contents:" >> /tmp/debug.output
pgrep -fl auto-restart.sh >> /tmp/debug.output
exit
fi
Run Code Online (Sandbox Code Playgroud)
此处完成脚本: https: //pastebin.com/bDyzxFXq
cron运行时,它使用以下方式/root/auto-restart.sh运行它:由于您\xe2\x80\x99已经使用了选项,因此在正在运行的进程\xe2\x80\x99命令行中查找任何位置;所以它也匹配。后者在 的输出中显示为普通。shsh -c /root/auto-restart.sh-fpgreppgrepauto-restart.shauto-restart.shsh -c /root/auto-restart.shshpgrep -l
pgrep -c auto-restart.sh\nRun Code Online (Sandbox Code Playgroud)\n\n会给你你\xe2\x80\x99所追求的行为。(我放弃了-l,因为它\xe2\x80\x99s 对 毫无意义-c。)
(你的服务器可能有一个看门狗计时器,这可能更合适\xe2\x80\x94,尽管我想象如果服务器仍然运行得足够好以运行cron作业,但被认为是关闭的,那么看门狗不会\xe2\ x80\x99t 行程。)
\n