Starting CRON work every 5 minutes in OpenShift (Red Hat Cloud)

I try 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 allows only for daily, hourly and daily scripts (putting the script in the appropriate folder).

I try 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 starts every minute (because it is placed in the thumbnail folder).

How can I overwrite it so that it starts every 5 minutes?

+7
cron openshift
source share
3 answers

Modify the script so that it checks the current time and frees it if it is not a multiple of 5 minutes.

Something like that:

 #!/bin/bash minute=$(date +%M) if [[ $minute =~ [05]$ ]]; then php ... fi 

The correct operand of the operator =~ is a regular expression; higher 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 0 or 5 ; $minute always exactly 2 characters).

(The theshadowmonkey user suggests in a comment:

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

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

+10
source share

I will continue Keith Thompson's answer:

His solution works fine every 5 minutes, but will not work, say, every 13 minutes; if we use $minutes % 13 , we get this schedule:

 5:13 5:26 5:30 5:52 6:00 because 0%13 is 0 6:13 ... 

I am sure you noticed this problem. We can achieve any frequency by counting minutes (hours, days or weeks) with Epoch :

 #!/bin/bash minutesSinceEpoch=$(($(date +'%s / 60'))) if [[ $(($minutesSinceEpoch % 13)) -eq 0 ]]; then php [...] fi 

date(1) returns the current date, we will format it as seconds with Epoch ( %s ), and then do the basic mathematical data:

 # .---------------------- bash command substitution # |.--------------------- bash arithmetic expansion # || .------------------- bash command substitution # || | .---------------- date command # || | | .------------ FORMAT argument # || | | | .----- formula to calculate minutes/hours/days/etc is included into the format string passed to date command # || | | | | # ** * * * * $(($(date +'%s / 60'))) # * * --------------- # | | | # | | Β·----------- date should result in something like "1438390397 / 60" # | Β·-------------------- it gets evaluated as an expression. (the maths) # Β·---------------------- and we can store it 

And you can use this approach with the hourly, daily or monthly cron job on OpenShift:

 #!/bin/bash # We can get the minutes=$(($(date +'%s / 60'))) hours=$(($(date +'%s / 60 / 60'))) days=$(($(date +'%s / 60 / 60 / 24'))) weeks=$(($(date +'%s / 60 / 60 / 24 / 7'))) # or even moons=$(($(date +'%s / 60 / 60 / 24 / 656'))) # passed since Epoch and define a frequency # let say, every 7 hours if [[ $(($hours % 7)) -ne 0 ]]; then exit 0 fi # and your actual script starts here 

Note that I used the -ne operator (not equal) to exit the script instead of using the -eq (equal) operator to transfer the script to the IF construct; It’s convenient for me.

And don't forget to use the correct .openshift/cron/{minutely,hourly,daily,weekly,monthly}/ folder for your frequency.

+8
source share

You can use available cron job . The work is carried out at a given frequency, and you can configure your work to check the date and / or time of the job.

Here is an example of starting a job every 5 minutes. Put the code snippet below into the executable file along the path .openshift/cron/minutely/awesome_job and give the appropriate permissions to chmod +x awesome_job , then add it to the application repository, commit and click.

Excerpt

 #!/bin/bash if [ ! -f $OPENSHIFT_DATA_DIR/last_run ]; then touch $OPENSHIFT_DATA_DIR/last_run fi if [[ $(find $OPENSHIFT_DATA_DIR/last_run -mmin +4) ]]; then #run every 5 mins rm -f $OPENSHIFT_DATA_DIR/last_run touch $OPENSHIFT_DATA_DIR/last_run # The 'awesome_command(s)' that you want to run every 5 minutes fi 

Explanation:

  • This snippet actually creates an empty file named "last_run" just to write down the last execution status and then start your work.
  • The next minute, your awesome_job will be executed, and now it will try to find the last_run file modified in the last 4 minutes. But your file "last_run" was created just a minute ago, and, therefore, the condition fails and ends. It continues.
  • In the fifth minute, while you find (with -mmin +4 ), your "last_run" will be displayed, and now the file "last_run" will be deleted and recreated and your amazing commands will be executed.

Source (white papers): https://developers.openshift.com/managing-your-applications/background-jobs.html#_execution_timing

0
source share

All Articles