OpenShift Cron Jobs

WARNING: This post was written when OpenShift 2 existed and is no longer relevant.

The following is a guide to using Cron in an OpenShift application.

OpenShift gives a simple out of the box solution to setup Cron using existing bash scripts.

Requirements

The following are required before reading the rest of this guide.

  • OpenShift account
  • OpenShift application installed
  • Git
  • SSH access

Setup

1. Run the following from the command-line to install Cron.

rhc cartridge add cron -a APP_NAME

NOTE: APP_NAME is the name of the application.

2. After installation, something similar to the following should be displayed to indicate that Cron has been installed successfully.

OpenShift Cron Installation Successful

3. Clone the application onto the local machine using Git.

4. Navigate to the Git project directory and there should be .openshift folder.

5. Navigate to .openshift/cron to view the following folders:

  • daily
  • hourly
  • minutely
  • monthly
  • weekly

Each folder represents how frequent the bash script is run, i.e. a bash script in the daily folder is run each day once.

Each folder can have any number of bash scripts that will all run at the specified frequency represented by the folder name.

6. Create the log-time.sh bash script in the following location .openshift/cron/minutely

 #!/bin/bash
 date >> ${OPENSHIFT_LOG_DIR}/log-time.log

The script will add a log entry with the current time.

$OPENSHIFT_LOG_DIR is the default location for logs. Refer to Log Files | OpenShift Developers.

7. Push the project using Git.

8. SSH to OpenShift and navigate to the Cron location, e.g. cd app-deployments/current/repo/.openshift/cron/minutely

9. Make the script executable, i.e. chmod +x log-time.sh.

10. Navigate to the $OPENSHIFT_LOG_DIR and check the log-time.log file entries are being added, i.e. tail log-time.log in an SSH terminal session.

or alternatively run rhc-tail-files -a APP_NAME -f APP_NAME/logs/log-time.log from the command-line (where APP_NAME is the application name).

References

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