Getting Started with CRON Jobs on OpenShift!/bin/bash

A feature that many users have requested is cron support. Many web applications and frameworks depend on the cron job scheduler to handle regular tasks such as cleaning up temp files, backing up databases, and generating reports. In this week’s update to Red Hat OpenShift, we have added cron support.

Here is a walk-though on how to enable and use cron.

First, we will create an application. We have added cron support to every application container type, so it doesn’t matter which one we use. Just for speed of startup, we will create a PHP application for this walk-through.

rhc app create cht php-5.3

And wait for it to run

Creating application: cht
blah blah blah
Successfully created application: cht

Now that we have the application created and running, let’s embed the new cron cartridge into it.

rhc cartridge add cron-1.4 -a cht

Most of what we need to know about using the cron cartridge is contained in the text that is displayed when it is successfully embedded:

cron-1.4 added to application cht
To schedule your scripts to run on a periodic basis, add the scripts to 
your application's .openshift/cron/{minutely,hourly,daily,weekly,monthly}/
directories (and commit and redeploy your application).
Example: A script .openshift/cron/hourly/crony added to your application
         will be executed once every hour.
         Similarly, a script .openshift/cron/weekly/chronograph added
         to your application will be executed once every week.

We can create UNIX scripts that will be run each minute, each hour, each day, each week, or each month, simply by placing them in the specified directory.

Let’s create a very simple script that will be run every minute.

 mkdir -p cht/.openshift/cron/minutely
 cd cht/.openshift/cron/minutely/
 cat >ticktock
 date >> ${OPENSHIFT_PHP_LOG_DIR}/ticktock.log
 chmod +x ticktock 
 cd ../../..

The bash script is named “ticktock”. Every time it is run by cron it will append the UNIX date string to the file “ticktock.log”, which will be in the logging directory for this application. Notice that we use the environment variable OPENSHIFT_PHP_LOG_DIR. This is specific to the type of application you created (php-5.3 used in this example). You should use the OpenShift environment variables instead of hardcoded paths, whenever possible.

Now let’s add the new script file to the local git repository, and then push it up to the OpenShift service.

 git add .openshift/cron/minutely/ticktock 
 git commit -a -m "add ticktock minutely cron script"
 git push

Now, go get a coffee, and check your email for a few minutes. We want some time to pass, so that cron will have run the script a couple of times.

Because we are writing into the OpenShift logging directory, we can use the rhc-tail-files command to look at it.

 rhc-tail-files -a cht -f cht/logs/ticktock.log

Which will output, after a short pause:

Attempting to tail files: cht/logs/ticktock.log
Use ctl + c to stop
Fri Feb  3 15:21:02 EST 2012
Fri Feb  3 15:22:03 EST 2012
Fri Feb  3 15:23:03 EST 2012
Connection to closed.

And that’s all there is to it!

As always, if you have any questions, feel free to ask them on the OpenShift Forums or come to IRC at and ask us the #openshift channel.

News, OpenShift Container Platform, OpenShift Dedicated, OpenShift Online, OpenShift Origin
Comments are closed.