Job scheduling on Linux with time randomization.....and efficiency
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:
- Schedule a job with cron that runs at a fixed time each day
- Have this job randomly select a time in the future
- Submit this new job once daily to the at scheduler
Step 1: Create a shell script to randomly select the time to run a job. The script might look something like this:
#!/bin/bashThe 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.
script="/data/runJob.sh" #insert the path to your script here
min=$(( 8 * 60 ))
rmin=$(( $RANDOM % $min ))
at -f "$script" now+${rmin}min
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/delayJob.shThis example schedules cron to trigger delayJob.sh 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!