Hi,

Cron is UNIX/Linux service or daemon to execute scheduled commands according to instruction in a file. It uses a daemon, crond, which runs constantly in the background and checks every minute/ hour/ day/ week/ month or an year (based on the configuration) to see if any of the scheduled jobs need to be executed; if so, it executes them. These jobs are generally referred to as cron jobs.

You can use the OpenShift Enterprise cron scheduler to set up timed jobs for your applications. This would need a running Openshift application, a cron cartridge (cron-1.4) and a cronjob script.

Follow Below steps to configure a cronjob:

Step-1: Create a new application:

$ rhc app create -a crontestapp -t php-5.3

Above will create a directory “crontestapp” at the location from where you ran the command from your terminal.

Step-2 : Add the cron scheduler cartridge to your new application, to configure the cronjobs :

rhc app cartridge add -a crontestapp -c cron-1.4

Step-3 : Add the cron jobs to your application depending on whether you want to execute them minutely, hourly, daily, weekly or monthly. for this navigate to :

${Your Project dir}/crontestapp/.openshift/cron/{minutely,hourly,weekly,daily,monthly}/

For example try below script:

..
#!/bin/bash
#This is a test cron script
#Author : Nikhil Mone
(echo ========== Hostname ========= ; hostname ; echo ========== Date ========== ; date ; echo ========== Grep PHP procs ========= ; ps -ef | grep php ; echo =========================) > $OPENSHIFT_REPO_DIR/php/foo.txt &
..

Step-4 : Now commit your changes and push them to the remote repository.

$ git add .openshift/cron/

$ git commit -m “configuring my test cron jobs”

$ git push

The script will write the output to the file foo.txt every minute. Now try to access your application from the browser and append “/foo.txt”

for eg :

http://crontestapp-mydomain.rhcloud.com/foo.txt

You can also use curl to verify the operation of this script:

..
$ curl http://crontestapp-mydomain.rhcloud.com/foo.txt

========== Hostname =========
ex-std-node69.prod.rhcloud.com
========== Date ==========
Sat Jan 12 13:55:04 EST 2013
========== Grep PHP procs =========
1269     27627     1  0 11:53 ?        00:00:02 /usr/sbin/httpd -C Include /var/lib/openshift/642c790205d04d7db91a368c5cbf04c2//php-5.3/conf.d/*.conf -f /usr/libexec/openshift/cartridges/php-5.3/info/configuration/etc/conf/httpd_nolog.conf -k start
1269     27629 27627  0 11:53 ?        00:00:00 /usr/sbin/rotatelogs /var/lib/openshift/642c790205d04d7db91a368c5cbf04c2/php-5.3/logs/error_log-%Y%m%d-%H%M%S-%Z 86400
1269     27630 27627  0 11:53 ?        00:00:00 /usr/sbin/rotatelogs /var/lib/openshift/642c790205d04d7db91a368c5cbf04c2/php-5.3/logs/access_log-%Y%m%d-%H%M%S-%Z 86400
1269     27652 27627  0 11:53 ?        00:00:00 /usr/sbin/httpd -C Include /var/lib/openshift/642c790205d04d7db91a368c5cbf04c2//php-5.3/conf.d/*.conf -f /usr/libexec/openshift/cartridges/php-5.3/info/configuration/etc/conf/httpd_nolog.conf -k start
1269     28774 28765  0 13:55 ?        00:00:00 grep php
=========================
..

The scripts that we put inside cron subdirectories get executed according to their frequencies and in alphabetical order. One need to make sure if you are using interdependent crons then the script names should be in alphabetical order.

You can use the below command to enable or disable your cron scripts. For example, to disable all of your scripts:

$ rhc app cartridge stop -a crontestapp -c cron-1.4

To enable all of your scripts:

$ rhc app cartridge start -a crontestapp -c cron-1.4

Note : The cron commands affect all cron jobs. You cannot disable or enable individual cron jobs.

Regards,
Middlewaremagic Team 🙂

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.