Wednesday, April 1, 2015

*Job scheduling on Linux with time randomization

NOTE: This page has moved to

Job scheduling on Linux with time randomization.....and efficiency

You might immediately wonder why I would write baout job scheduling and randomization.  I understand that this does appear to be two entirely opposite ideas - and in a lot of cases you would be completely correct.

However, there are also some very valid use cases for having a scheduled job that happens at a random time.  Most of these use cases revolve around resource scheduling and potential performance hits.  There are also situations where timing is not particularly important.

On Linux, there is the cron daemon which is very good at scheduling jobs.  Unfortunately, however, there is no way to say to cron "please run this job at 8am plus or minus 2 hours"

There is another job scheduling daemon on Linux called at which is useful here.  The at daemon is used to schedule a job to run once in the future.

By combining cron and at we can create a job scheduling technique that allows for randomization (or other programmatic time selection each day)

To make this happen, you will:     
  1. Schedule a job with cron that runs at a fixed time each day
  2. Have this job randomly select a time in the future
  3. Submit this new job once daily to the at scheduler
Don't worry, it isn't as complicated as it sounds.  Here is how you can easily do this.

Step 1: Create a shell script to randomly select the time to run a job.  The script might look something like this:
script="/data/" #insert the path to your script here
min=$(( 8 * 60 ))
rmin=$(( $RANDOM % $min ))
at -f "$script" now+${rmin}min
The 2 important lines in this file are the boldfaced line above.  The first line selects the script you want to run at the random time in the future.

The second line sets the limit for how far in the future your randomized job time will be.  In this example, I have set the limit to 8 hours.  The line underneath the boldfaced line picks a random minute number between 0 and 8 hours.  The final line submits the job to the at scheduler

Step 2: Edit your crontab file by using the crontab command and add your new fixed time job.
   0  8 * * * /data/
This example schedules cron to trigger at 8am every day

You are finished - you now have a job that will run each day at a random time between 8am and 4pm (since I set an 8 hour window)

You can change the time calculation function to be whatever you want.

The best part....this method does not create inefficient timers, sleep functions and will survive a system reboot no matter when it happens.

Enjoy your planned chaos!