Run Cron Jobs at Arbitrary Intervals
Current versions of cron support sophisticated syntax making creating weird schedules a breeze. With older versions of cron you normally had to schedule multiple jobs to fill every time slot in a complex schedule. The most common of uncommon requirements is to run a cron job every so many minutes.
To run a cron job, say, every 55 minutes, these days you can just schedule “*/55 * * * *” you’ll be good to go. On older Unix systems you’ll encounter an error, such as “unexpected character found in line” in Solaris 10. The solution is to create a wrapper script and rely on epoch time to calculate time intervals.
Here’s a simple example: let’s say every 55 minutes you need to “date >> /var/adm/messages“. First, you create the wrapper script (say, /var/adm/bin/date_cron_wrapper.sh):
#!/bin/bash m=55 [ $(( `/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);print $2}'` / 60 % $m )) -eq 0 ] && date >> /var/adm/messages
The weirdness you see with “truss” and “nawk” is due to lack of epoch option for the “date” command on Solaris 10. On systems that do support “date +’%s’“, you can simply do this:
#!/bin/bash m=55 [ $(( `date +'%s'` / 60 % $m )) -eq 0 ] && date >> /var/adm/messages
Now you can schedule the cron job:
0-59 * * * * /var/adm/bin/date_cron_wrapper.sh >/dev/null 2>&1
The script will run a modulo operation on the current epoch time to see if the remainder after dividing by 3300 seconds (55 minutes) is zero. If that’s the case, then 55 minutes have passed and the wrapper script will execute its payload. You can get away with running this cron job every five minutes, instead of every minute.
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /var/adm/bin/date_cron_wrapper.sh >/dev/null 2>&1