Solved

Cron Scheduling

Posted on 2013-06-25
3
295 Views
Last Modified: 2013-09-19
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.
0
Comment
Question by:U_S_A
3 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 39275609
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)
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39275623
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))
0
 
LVL 5

Accepted Solution

by:
DOSLover earned 500 total points
ID: 39276041
This link may be useful: http://stackoverflow.com/questions/8181949/how-do-set-cron-to-run-my-script-every-40mins-25mins
In essense, you will need to set up multiple cron entries, for example, for the same job. The job that needs to be run every 16 minutes will have 4 cron jobs - the first one, RPOnlinePurge, can be scheduled to run at 1st, 17th, 33rd and 49th minutes of hour 1, 5, 9, 13. The same job again can be scheduled to run at 5th, 21st,37th,53rd minutes of hours 2,6,10,14,18,22 and so on.
Something like this:
1,17,33,49 1,5,9,13,17,21 * * *     your-command-for-RPOnlinePurge
05,21,37,53 2,6,10,14,18,22 * * *     your-command-for-RPOnlinePurge
....
....
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now