句柄“竞争条件”之间2 cron任务。什么是最好的方法? [英] Handle "race-condition" between 2 cron tasks. What is the best approach?

查看:175
本文介绍了句柄“竞争条件”之间2 cron任务。什么是最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定期运行的cron任务。此任务取决于有效的条件以完成其处理。在这种情况下,这个条件只是数据库中特定记录的SELECT。如果条件不满足(即SELECT不返回预期的结果集),那么脚本立即退出。

这是坏的,因为条件将很快有效(不知道多久,但是它将有效,因为运行另一个脚本)。

所以我想以某种方式使脚本更健壮。我想到了2个解决方案:


  1. 放置循环和 sleep ,直到条件为
    有效。这应该工作,但它有缺点,一旦脚本
    在循环,它是失控的。所以我虽然额外
    醒来后检查是一个特定的文件存在。如果它这样做,
    理解用户想要强制停止它。

  2. 脚本确定条件无效 之后
    在crontab中附加脚本并停止。该秒脚本
    持续轮询该条件,如果条件有效
    ,然后重新启动第一个脚本以重新启动其处理。这个解决方案对我来说似乎工作,但我不知道如果它是一个很好的解决方案。例如。也许编程修改crontab是一个坏主意?

无论如何,我认为这个问题很常见,可能有一个标准的解决方案,比我想出的更好。有人有更好的建议吗?从我的想法是最好的?

解决方案

继续我们的对话在评论中,您可以利用条件执行 cron条目。假设您希望根据一天中的时间进行分支,则可以使用 日期



例如:这将总是调用第一个命令,然后调用第二个命令,目前为11:

  echo'ScriptA running'; [$(date +%H)== 11]&& echo'ScriptB running'






更多示例! p>

要检查第一个命令的返回值:

  '; [$? == 0] echo'ScriptB'

要检查STDOUT,可以使用冒号作为noop 和分支通过捕获输出与相同的 $()构造我们使用 date

 :; [$(echo'ScriptA')=='ScriptA']&& echo'ScriptB'

最后一个例子的一个缺点:第一个命令的STDOUT不会被打印到控制台。你可以把它捕获到一个变量,你 echo 出来,或者写它到一个文件与 tee


I have a cron task that runs periodically. This task depends on a condition to be valid in order to complete its processing. In case it matters this condition is just a SELECT for specific records in the database. If the condition is not satisfied (i.e the SELECT does not return the result set expected) then the script exits immediately.
This is bad as the condition would be valid soon enough (don't know how soon but it will be valid due to the run of another script).
So I would like somehow to make the script more robust. I thought of 2 solutions:

  1. Put a while loop and sleep constantly until the condition is valid. This should work but it has the downside that once the script is in the loop, it is out of control. So I though to additionally after waking up to check is a specific file exists. If it does it "understands" that the user wants to "force" stop it.
  2. Once the script figures out that the condition is not valid yet it appends a script in crontab and stops. That seconds script continually polls for the condition and if the condition is valid then restart the first script to restart its processing. This solution to me it seems to work but I am not sure if it is a good solution. E.g. perhaps programatically modifying the crontab is a bad idea?

Anyway, I thought that perhaps this problem is common and could have a standard solution, much better than the 2 I came up with. Does anyone have a better proposal? Which from my ideas would be best? I am not very experienced with cron tasks so there could be things/problems I could be overseeing.

解决方案

Following up from our conversation in comments, you can take advantage of conditional execution in a cron entry. Supposing you want to branch based on time of day, you might use the output from date.

For example: this would always invoke the first command, then invoke the second command only if the clock hour is currently 11:

echo 'ScriptA running' ; [ $(date +%H) == 11 ] && echo 'ScriptB running'


More examples!

To check the return value from the first command:

echo 'ScriptA' ; [ $? == 0 ] echo 'ScriptB'

To instead check the STDOUT, you can use as colon as a noop and branch by capturing output with the same $() construct we used with date:

: ; [ $(echo 'ScriptA') == 'ScriptA' ] && echo 'ScriptB'

One downside on the last example: STDOUT from the first command won't be printed to the console. You could capture it to a variable which you echo out, or write it to a file with tee, if that's important.

这篇关于句柄“竞争条件”之间2 cron任务。什么是最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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