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:
The right operand of the
=~
operator is a regular expression; the above matches if the current minute ends in0
or5
. Several other approaches are possible:(check for any character followed by a
0
or5
;$minute
is always exactly 2 characters).(User theshadowmonkey suggests in a comment:
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 constant08
or09
is an error. You could work around this withsed
, but it's probably not worthwhile given that there are other solutions.)I will extend Keith Thompson answer:
His solution works perfectly for every 5 minutes but won't work for, let's say, every 13 minutes; if we use
$minutes % 13
we get this schedule:I'm sure you notice the issue. We can achieve any frequency if we count the minutes(, hours, days, or weeks) since Epoch:
date(1)
returns current date, we format it as seconds since Epoch (%s
) and then we do basic maths:And you may use this approach with hourly, daily, or monthly cron jobs on OpenShift:
Notice that I used the
-ne
(not equal) operator to exit the script instead of using the-eq
(equal) operator to wrap the script into the IF construction; I find it handy.And remember to use the proper
.openshift/cron/{minutely,hourly,daily,weekly,monthly}/
folder for your frequency.You can make use of minutely cron job available. The jobs are run at a specified frequency and you can instrument your job to inspect the date and/or time when your job runs.
Here is an example for running a job every 5 minutes. Place the below code snippet in an executable file at path
.openshift/cron/minutely/awesome_job
and give proper permissionschmod +x awesome_job
then add it to your application repository, commit and push.The snippet
Explanation:
-mmin +4
), your 'last_run' will be shown and now, the file 'last_run' will be deleted and recreated and your awesome command(s) will be executed.Source (Official Docs): https://developers.openshift.com/managing-your-applications/background-jobs.html#_execution_timing