仅当 cron 作业尚未运行时才运行它 [英] Run cron job only if it isn't already running

查看:26
本文介绍了仅当 cron 作业尚未运行时才运行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 cron 作业设置为我创建的守护程序的一种看门狗.如果守护程序出错并失败,我希望 cron 作业定期重新启动它......我不确定这有多大可能,但我阅读了几个 cron 教程,但找不到任何可以做我想做的事情我在找……

I'm trying to set up a cron job as a sort of watchdog for a daemon that I've created. If the daemon errors out and fails, I want the cron job to periodically restart it... I'm not sure how possible this is, but I read through a couple of cron tutorials and couldn't find anything that would do what I'm looking for...

我的守护进程从一个 shell 脚本启动,所以我真的只是在寻找一种方法来运行一个 cron 作业,前提是该作业的前一次运行没有仍在运行.

My daemon gets started from a shell script, so I'm really just looking for a way to run a cron job ONLY if the previous run of that job isn't still running.

我发现了这篇文章,它确实为我尝试使用锁定文件所做的事情提供了解决方案,而不是我不确定是否有更好的方法来做到这一点...

I found this post, which did provide a solution for what I'm trying to do using lock files, not I'm not sure if there is a better way to do it...

推荐答案

我为我编写的打印后台处理程序执行此操作,它只是一个 shell 脚本:

I do this for a print spooler program that I wrote, it's just a shell script:

#!/bin/sh
if ps -ef | grep -v grep | grep doctype.php ; then
        exit 0
else
        /home/user/bin/doctype.php >> /home/user/bin/spooler.log &
        #mailing program
        /home/user/bin/simplemail.php "Print spooler was not running...  Restarted." 
        exit 0
fi

它每两分钟运行一次,非常有效.如果由于某种原因进程没有运行,我会通过电子邮件向我发送特殊信息.

It runs every two minutes and is quite effective. I have it email me with special information if for some reason the process is not running.

这篇关于仅当 cron 作业尚未运行时才运行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆