Background Tasks in Jenkins and IP-change Script Example

| Comments

There are a few ways to schedule tasks to run periodically, from building our own scheduler in whatever language we want, to use chroniker in Django, or even use the built-in cron utility in Unix systems.

However, I found out that Jenkins is quite good with that, even though that is not its main objective as CI system. These are the advantages of Jenkins over cron.

  • Embedded email system on failures.
  • See logs (and embedded log rotation).
  • See clearly when they ran for the last time.
  • Friendly web interface.
  • Keep a history of all runs.
  • Take the latest version directly from git/cvs/etc.
  • Lot of plugins.
  • Setup a different number of executors so it doesn’t overload the system (best for low-priority processes).

Since all of those features come out of the box, you simply need to setup a job specifying the script you want to run and it’s frequency, as you would do with Cron.

Sample configuration: IP change

We are going to show one of our background scripts we have running on Jenkins and its configuration. This is just a sample, and you can modify it in any way you want (e.g. using git directly rather than downloading the project first in a different task).

The script we want to show is a simple python script that checks if the IP has changed locally (useful for dynamic IP environments). As the script is returning an error when it changes, we get an email everytime it happens, but we could also update our dynamic DNS using services like changeip.com if your router doesn’t provide that feature.

In order to run that script, we have a lot of options here:

  • Write the script directly in the Jenkins task in the default Jenkins terminal (sh by default).
  • Copy the script to any folder and just run it from Jenkins (like python $FOLDER/run.py)
  • Fetch from a git and run it.
  • Create a task to fetch the project (based on git changes), and another task to run it (from the previous fetched folder).

In this case we are going to use the last option (the one with 2 tasks), the reason to do this is because we don’t update that script very often, so it is better to fetch it once and run it 1 hundred times; and another reason is because sometimes the command git fetch sometimes fails in jenkins, generating a false positive (apparently this is a bug in the jenkins git plugin)

Configuring the fetch task

This part is basic if you have set up a Jenkins job before using git, if not, just copy the following config using our IP Change repo available in GitHub

This project doesn’t require any aditional config, as the emails are sent by Jenkins and your files and IPs are generated by the script itself.

Once the task has ran, your ip_change.py script should be located in /var/lib/jenkins/jobs/IP Change fetch/workspace, and you could try to run it directly from the terminal using the jenkins user (sudo -u jenkins python ip_change.py)

Configuring the background task

Now is time to setup your background task using this configuration.

Where $HOME or $JENKINS_HOME is an environment variable that contains /var/lib/jenkins by default

Then try to run the new background task to see any of the following results:

  • Running the task for the first time:
1
2
3
4
5
6
+ cd '/var/lib/jenkins/jobs/IP Change fetch/workspace'
+ python ip_change.py
* Requesting current ip with 'icanhazip'
* Request took 0 seconds 
* This is the first time to run the ip_change script, I will create a file in /tmp/ip.log to store your current address: 86.185.119.138 
Finished: SUCCESS
  • IP is still the same:
1
2
3
4
5
6
+ cd '/var/lib/jenkins/jobs/IP Change fetch/workspace'
+ python ip_change.py
* Requesting current ip with 'icanhazip'
* Request took 0 seconds 
* IP is still the same: 86.185.119.138
Finished: SUCCESS
  • IP has changed (it will send an email):
1
2
3
4
5
6
7
8
+ cd '/var/lib/jenkins/jobs/IP Change fetch/workspace'
+ python ip_change.py
* Requesting current ip with 'icanhazip'
* Request took 0 seconds 
* IP has changed from 86.185.224.178 to 86.185.119.138
Build step 'Execute shell' marked build as failure
Sending e-mails to: <your-email>@<your-domain>
Finished: FAILURE

Conclussions

There are also a lot of useful things you can run this way like daily backups or server health-checks.

So if you already have a Jenkins server for your common CI tasks, why are you not using it for job schedulling too ?

Comments