在OpenShift(Red Hat Cloud)上每5分钟运行一次CRON作业 [英] Run CRON job every 5 minutes on OpenShift (Red Hat Cloud)

查看:279
本文介绍了在OpenShift(Red Hat Cloud)上每5分钟运行一次CRON作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每5分钟运行一次此脚本。似乎在OpenShift上运行CRON作业的唯一方法是使用它们的CRON插件。而且CRON插件只允许每分钟,每小时和每天的脚本(通过将脚本放在相应的文件夹中)。



我试图每5分钟运行一次这个脚本:

 #!/ bin / bash 
php / var / lib / openshift / 53434c795973ca1ddc000668 / app-root / runtime /repo/scheduled.php> / dev / null 2>& 1

但现在它运行每一分钟



如何重写它,使其每5分钟运行一次? >解决方案

修改脚本,以便检查当前时间,如果不是5分钟的倍数,则退出。



 #!/ bin / bash 

minute = $(date +%M)
if [[$ minute =〜[05] $]]; then
php ...
fi

=〜运算符是正则表达式;如果当前分钟以 0 5 结束,则上述匹配。几种其他方法是可能的:

  if [[$ minute =〜。[05]然后

(检查任何字符后跟 0 5 ; $ minute 总是正好为2个字符)。



(用户theshadowmonkey在评论中建议:

  if [$(($ minute%5))-eq 0];然后是

它将算术检查 $ minute 是5的倍数,但是有一个问题在 $((...))表达式中,带有前导零的常量被视为八进制;如果当前时间是8或9分钟,常量 08 09 使用 sed 解决这个问题,但考虑到还有其他解决方案,可能不值得。)


I'm trying to run this script every 5 minutes. It seems the only way to run CRON jobs on OpenShift is to use their CRON plugin. And the CRON plugin only allows for minutely, hourly, and daily scripts (by placing the script in the corresponding folder).

I am trying to run this script every 5 minutes:

#!/bin/bash
php /var/lib/openshift/53434c795973ca1ddc000668/app-root/runtime/repo/scheduled.php > /dev/null 2>&1

But right now it runs every minute (because it's placed in the minutely folder).

How can I re-write it so that it runs every 5 minutes?

解决方案

Modify the script so it checks the current time, and bails out if it's not a multiple of 5 minutes.

Something like this:

#!/bin/bash

minute=$(date +%M)
if [[ $minute =~ [05]$ ]]; then
    php ...
fi

The right operand of the =~ operator is a regular expression; the above matches if the current minute ends in 0 or 5. Several other approaches are possible:

if [[ $minute =~ .[05] ]]; then

(check for any character followed by a 0 or 5; $minute is always exactly 2 characters).

(User theshadowmonkey suggests in a comment:

if [ $(($minute % 5)) -eq 0 ]; then

which checks arithmetically whether $minute is a multiple of 5, but there's a problem with that. In the expression in a $(( ... )) expression, constants with leading zeros are treated as octal; if it's currently 8 or 9 minutes after the hour, the constant 08 or 09 is an error. You could work around this with sed, but it's probably not worthwhile given that there are other solutions.)

这篇关于在OpenShift(Red Hat Cloud)上每5分钟运行一次CRON作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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