Scheduling timed jobs using cron on Openshift


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:

#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 :

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

$ curl

========== Hostname =========
========== 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.

Middlewaremagic Team 🙂

Application Monitoring using MongoDB Monitoring Service (MMS) on Openshift


In this article we will discuss about monitoring the appliation which are deployed over Openshift. We will use MongoDB Monitoring Service (MMS) to achieve this. Application monitoring is very important for developers before and after deploying the applications in production, let that be performance measurement, some tuning or random health checks.

The MongoDB Monitoring Service (MMS) is a cloud-based monitoring service, designed by 10gen, to monitor the health of MongoDB deployments. MMS provides an agent that runs on MongoDB servers, and this agent reports information such as opcounters, memory information, number of connections, network I/O, database storage size, and more. All of this information is available in customizable charts that are displayed in your web browser.

Openshift provides us a cartridge that can be used for monitoring the MongoDB apps. Ofcourse you would need to add a MongoDB cartridge as well, but this doesn’t mean that you can not use any other databse. 🙂

I tried a sample app that uses mysql database and I was still able to get the monitoring set up for that. 🙂

Below are the steps that you need to follow to have MMS setup :

Step-1: Create a test application (in this example a java app) :

$ rhc app create -a monitoringDemo -t jbossas-7

Step-2 : Add MongoDB cartridge to above application :

$ rhc app cartridge add -a monitoringDemo -c mongodb-2.0

Step-3 : Login to and create an account.

Step-4 : Once done download the mms agent by clicking on “download the agent” link on the Hosts page. Extract the zip file, you will get a dir as “mms-agent”.

Step-5 : Open a terminal and navigate to your project directory, from where you executed Step-1.

Step-6 : You will see a directory created as “monitoringDemo”, move upto “.openshift” inside it :

[userone@userone monitoringDemo]$ ls -ltra
total 44
-rw-rw-r--.  1 userone userone    7 Jul 25 18:58 .gitignore
drwxrwxr-x.  3 userone userone 4096 Jul 25 18:58 src
-rw-rw-r--.  1 userone userone 1695 Jul 25 18:58 pom.xml
-rw-rw-r--.  1 userone userone   56 Jul 25 19:01
-rwxrwxr-x.  1 userone userone    0 Jul 25 19:09 .gitkeep
-rwxrwxr-x.  1 userone userone    0 Jul 25 19:09 mysql-connector-java-5.1.13-bin.jar.dodeploy
-rw-rw-r--.  1 userone userone 7629 Jul 25 19:09 README
drwxrwxr-x.  4 userone userone 4096 Jul 25 19:10 deployments
drwxr-xr-x.  6 userone userone 4096 Jul 25 19:10 .
drwxrwxrwx. 24 userone userone 4096 Aug  2 19:41 ..
drwxrwxr-x.  7 userone userone 4096 Aug  7 14:54 .openshift
drwxrwxr-x.  8 userone userone 4096 Aug  7 14:56 .git
[userone@userone monitoringDemo]$ pwd
[userone@userone monitoringDemo]$ cd .openshift/

Step-7 : Create a new directory as “mms” :

[userone@userone .openshift]$ mkdir mms
[userone@userone .openshift]$ ls -ltra
total 28
drwxrwxr-x. 7 userone userone 4096 Jul 25 18:58 cron
drwxrwxr-x. 3 userone userone 4096 Jul 25 18:58 config
drwxrwxr-x. 2 userone userone 4096 Jul 25 18:58 action_hooks
drwxrwxr-x. 2 userone userone 4096 Jul 25 18:58 markers
drwxr-xr-x. 6 userone userone 4096 Jul 25 19:10 ..
drwxrwxr-x. 7 userone userone 4096 Aug  7 14:54 .
drwxrwxr-x. 2 userone userone 4096 Aug  7 14:55 mms
[userone@userone monitoringDemo]$ pwd

Step-8 : After step-4 you would have got a directory as “mms-agent” navigate inside it and copy “” file to “/myProjDir/monitoringDemo/.openshift/mms”

Note : Make sure you do that, or else MMS won’t work.

Step-9 : Navigate back to project directory add the changes to git repo, commit those changes and then push:

$ cd /myProjDir/monitoringDemo/

$ git add .openshift/mms/

$ git commit -a -m “some commit message”

$ git push

Step-10 : Now add the MMS agent cartridge to your application :

$ rhc app cartridge add -a monitoringDemo -c 10gen-mms-agent-0.1

Phewww setup is almost done, now lets make it work 🙂

Step-11 : ssh to your application :

$ ssh

for eg :


and run below command :



* admin = MongoDB username
* ABCD8YrgQth = password
* = IP address of your MongoDB host/node

Step-12 : Navigate to and click the Hosts + button at the top of the page and add all the details from the output of Step-11.

Step-13 : Click the name of the host that you want to monitor to view the available data collection streams. First time it will take some time as it needs to schedule the checks.

This is not over yet, you can also configure multiple alerts and can can customize your view according to your requirement. Pretty simple,easy, open-source monitoring 🙂

Middlewaremagic Team

Continuous Integration on OpenShift using Jenkins


In this article we will discuss about Continuous Integration. What it is? Why to use it? and how to integrate the this model on OpenShift.

While working on a code change, a developer takes a copy of the current code base on which to work. As other developers submit changed code to the source code repository, this copy gradually does not reflect the repository code. There may be addition/removal of new/old libraries and resources, that the developer may not be aware of and can result in potential conflicts.

The longer a branch of code remains checked out, the greater the risk of multiple integration conflicts and failures becomes when it is reintegrated into the main line. When developers submit code to the repository they must first update their code to reflect the changes in the repository since they took their copy. The more changes the repository contains, the more work developers must do before submitting their own changes. In a worst-case scenario, developers may have to discard their changes and completely redo the work.

Continuous integration involves integrating early and often, so as to avoid above issues. The practice aims to reduce rework and thus reduce cost and time. With CI each programmer must do a complete build and run (and pass) all unit tests before submitting their work. Integration tests are usually run automatically on a CI server when it detects a new commit.

Lets see how we can use jenkins on Openshift :

Step-1 :

We will create an application using jbossas-7 web cartridge with jenkins enabled. This will eventually create two applications in your OpenShift account, one as jenkins and one as your app.

$ rhc app create -a $application name -t jbossas-7 –enable-jenkins

for example :

[userone@userone OpenShift]$ rhc app create -a testjenkins -t jbossas-7 --enable-jenkins
Password: ********

Creating application: jenkins in userone
Now your new domain name is being propagated worldwide (this might take a minute)...
Confirming application 'jenkins' is available:  Success!

jenkins published:
git url:  ssh://

Jenkins created successfully.  Please make note of these credentials:

   User: admin
   Password: CgyLzMVUzVg3

Note:  You can change your password at:

Creating application: testjenkins in userone
Now your new domain name is being propagated worldwide (this might take a minute)...
Warning: Permanently added the RSA host key for IP address '' to the list of known hosts.
Now embedding the jenkins client into 'testjenkins'...
Confirming application 'testjenkins' is available:  Success!

testjenkins published:
git url:  ssh://
Successfully created application: testjenkins

Note : Make sure to save the credentials for Jenkins server.

Step-2 :

Log in to Jenkins console by navigating to : https://jenkins-yourdomain

You will see a build with your application name.


Step-3 :

Now open a terminal on your local machine and navigate to the directory from where you ran Step-1. There will be a directory with your application name.

$ cd testjenkins/deployments

Now put a sample application (war/ear) inside it. You can also put an exploded war/ear but just make sure to create an empty file as “yourapplication.war.dodeploy”.

for eg :

[userone@userone deployments]$ pwd
[userone@userone deployments]$ ls -ltra
total 12
-rw-rw-r--. 1 userone userone    0 Jul 21 12:47 .gitkeep
drwxr-xr-x. 6 userone userone 4096 Jul 21 12:47 ..
-rw-rw-r--. 1 userone userone    0 Jul 21 13:05 PostDataDemo.war.dodeploy
drwxrwxr-x. 3 userone userone 4096 Jul 21 13:05 .
drwxrwxr-x. 4 userone userone 4096 Jul 21 13:07 PostDataDemo.war

Step-4 :

Now push the changes to OpenShift repository :

$ cd $yourappdir

$ git add .

$ git commit -a -m “my first commit”

$ git push

Once we push the changes you will see a build is getting scheduled on Jenkins server and you can track it from Jenkis console (mentioned in Step-2)

for eg:

[userone@userone testjenkins]$ git add .

[userone@userone testjenkins]$ git commit -a -m "unexploded commit"
[master e08e8bc] unexploded commit
 6 files changed, 49 insertions(+), 0 deletions(-)
 delete mode 100644 deployments/PostDataDemo.war
 create mode 100644 deployments/PostDataDemo.war.dodeploy
 create mode 100644 deployments/PostDataDemo.war/META-INF/MANIFEST.MF
 create mode 100644 deployments/PostDataDemo.war/WEB-INF/web.xml
 create mode 100644 deployments/PostDataDemo.war/index.jsp
 create mode 100644 deployments/PostDataDemo.war/logout.jsp
 create mode 100644 deployments/PostDataDemo.war/test.jsp

[userone@userone testjenkins]$ git push
Counting objects: 14, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (11/11), 1.25 KiB, done.
Total 11 (delta 1), reused 0 (delta 0)
remote: Executing Jenkins build.
remote: You can track your build at
remote: Waiting for build to schedule....Done
remote: Waiting for job to complete.........................................................Done
remote: SUCCESS
remote: New build has been deployed.
To ssh://
   8e4fe25..e08e8bc  master -> master

From console you can see :

The build is successful.

Step-5 :

Now try to access your application. Just make sure if you have given a context root for your application you need to add it in front of your openshift app url. for eg :

where “PostDataDemo” was context root of my application.

So from here you can make changes in any part of your code and just follow step-4. If by any chance there are any issues in code or build fails.. need not to worry as when build fails the content will not be deployed and hence won’t affect your running application.

In case of any issue, you can tail jenkins or application logs, using below command :

$ rhc app tail -a jenkins

$ rhc app tail -a $yourapplication name

Also you can check Jenkins build console output, by logging in Jenkins server from browser.

You can always tweak in your Jenkins configuration to have added functionalities. 🙂

MiddlewareMagic Team 🙂

Copyright © 2010-2012 Middleware Magic. All rights reserved. |