Monday, February 22, 2010

Running and monitoring batch processes in Hudson/Jenkins

Hudson (renamed to Jenkins) is not only an excellent tool for running and monitoring software builds, but also for running any periodic process: software updates, batch and maintenance scripts, backups, etc.

Last week I participated in a 'javahispano.org' podcast where I talked about the goodness of Continuous Integration and Hudson, and I said that it is recommendable to move most automatic maintenance scripts from crontabs to Hudson.

This can be easily achieved by writing a simple shell script which contains the set of processes to execute periodically and which saves results into junit reports, so that Hudson will be able to record the exit status, the standard and error outputs,  and the time lasted by each command, as is shown in this screenshot:



I use a simple shell script library I wrote time ago. It generates a junit report using the same technique that surefire does in maven putting all xml files in a folder. You can get the script library from here.

The usage of the library is very easy, just include it at the beginning of your shell script and call the method 'juLog' each time you want to report anything to Hudson.  I think a simple example is a bit more instructive:

#/bin/sh

#### Include the library
. `dirname $0`/sh2ju.sh

#### Clean old reports
juLogClean
#### Success command
juLog  -name=myTrueCommand true
#### Failure
juLog  -name=myFalseCommand false
#### Sleep
juLog  -name=mySleepCommand sleep 5
#### The test fails because the word 'world' is found in command output
juLog  -name=myErrorCommand -ierror=world   echo Hello World
#### A sql command
juLog  -name=mySqlCommand psql -h localhost -U postgres -c 'select now()'
#### A call to a customized method
myCmd() {
   ls -l $*
   return 0
}
juLog  -name=myCustomizedMethod myCmd '*.sh'


Finally configure Hudson folowing these steps:
  • Create a free-style project 

  • Put your script and the library in SCM

  • Chosse execute a shell as build method

  • Run it periodically

  • Publish Junit results