只运行,如果它尚未运行cron作业 [英] Run cron job only if it isn't already running

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

问题描述

所以我想成立一​​个cron作业作为一种看门狗为我创建了一个守护进程。如果守护程序出现了错误和失败,我希望cron作业定期重新启动它...我不知道怎么可能是这样的,但我通过一对夫妇的cron教程阅读并找不到任何会做什么,我在寻找...

So 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作业,只有在该作业的previous运行没有仍在运行。

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天全站免登陆