首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cron作业不会运行(pgrep问题)

Cron作业不会运行(pgrep问题)
EN

Unix & Linux用户
提问于 2018-10-12 07:44:14
回答 1查看 2.1K关注 0票数 3

我每分钟运行一次cron作业,这将启动bash shell脚本:

代码语言:javascript
复制
# Automatically reboot the server if it goes down
* * * * * /root/auto-restart.sh

我可以手动运行脚本,但是每当我尝试通过cron作业运行它时,它就不会运行。我做了一些调试,发现这个命令是罪魁祸首:

代码语言:javascript
复制
pgrep -fcl auto-restart.sh

总之,当手动运行时,该命令返回正确的值,并且只在脚本当前运行时列出auto-restart.sh (正如它应该的那样)。

但是,当cron作业启动它时,它将显示pgrep -fl auto-restart.sh的输出,如下所示(当它运行时):

代码语言:javascript
复制
3110 sh
3111 auto-restart.sh

下面是我正在使用的代码:

代码语言:javascript
复制
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

EN

回答 1

Unix & Linux用户

发布于 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.shsh -c /root/auto-restart.sh。后者在sh的输出中显示为普通的pgrep -l

代码语言:javascript
复制
pgrep -c auto-restart.sh

会给你你想要的行为。(我放弃了-l,因为它对-c没有意义。)

(您的服务器可能有一个看门狗定时器,这可能更合适--尽管我认为,如果服务器仍然运行得足够好,可以运行cron作业,但在其他情况下被认为处于瘫痪状态,那么看门狗就不会绊倒。)

票数 5
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/474990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档