Link to home
Start Free TrialLog in
Avatar of U_S_A
U_S_A

asked on

Cron Scheduling

Hi,

I'm trying to get three cron jobs to run incrementally throughout the day. I use contab to schedule some jobs on my linux server.

Goals:
- I don't want the jobs to run at the same time as each other
- I want the jobs to run ever 16 minutes (or 17 or 18 - see below).  I'm trying to avoid having the jobs all running at the same time ever hour and especially I don't want them to run at the top of the hour, every hour.  It's ok if the jobs rotate into the top of the hour once in awhile.

Here is the cron entries
*/16 * * * *     (cd ~rp/public_html/special_tools && ./online_purge.php purge) 2>&1 | mail -s "RP Online Purge" rp-admin
*/17 * * * *     (cd ~fp/public_html/special_tools && ./online_purge.php purge) 2>&1 | mail -s "FP Online Purge" rp-admin
*/18 * * * *     (cd ~ar/public_html/special_tools && ./online_purge.php purge) 2>&1 | mail -s "AR Online Purge" rp-admin

Open in new window


Problem is that the jobs all run seem to start at the top of the hour and rotate, but start over at the top of hour,, because I guess I have not scheduled the cron correctly.

See this print screen.  It shows when the jobs run.
http://screencast.com/t/cUZA7uyPD

I hope the jobs run like this...
16 mins, then runs again in 16 mins, then again in 16 mins, etc, etc, etc

I appreciate your help

Thank you.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

can you run a script that run the commands you want then sleep for some time? e.g.

while true
do
   command1
    command2
   sleep 600
done

the above script will run for ever and as yo can see will sleep for 600 seconds (10 min)
To achieve what you want you should not specify the starting minute as an asterisk.

Specify the desired minute plus the interval instead, like

3/18 * * * *
7/17 * * * *
11/16 * * * *

The first job will run at 3/21/39/57 minutes past the hour
The second job will run at 7/24/41/58.minutes past the hour
The third job will run at 11/27/43/59  minutes past the hour

>>  avoid having the jobs all running at the same time ever hour <<

This will unfortunately not be possible with cron alone.

You could add some scatter by specifying a random sleep interval at the top of your scripts, let's say something between 0 and 5 minutes:

sleep $(($RANDOM%300))
ASKER CERTIFIED SOLUTION
Avatar of DOSLover
DOSLover
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial