Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've got two scripts in cron set to run every two minutes: */2 -- the thing is, they're out of step. One runs at 1,3,5,7,9 minutes, etc. and the other at 0,2,4,6,8. This is not a mission-critical problem, but means I've got two status reports, one a bit stale compared to the other.

What does cron do exactly? Run the first one in crontab document order, waiting till it's finished to run the second one?

Is there any way I can make the run at the same time, or as close as possible?

share|improve this question

migrated from stackoverflow.com Apr 12 '10 at 17:07

This question came from our site for professional and enthusiast programmers.

add comment

4 Answers

Sounds to me like your best bet is to combine the two scripts into one to ensure that they are run at nearly identical times.

share|improve this answer
add comment

The job will be run as close to the given time as possible, but may be run slightly later. As Chris suggested, the best thing to do is probably create a small shell script and schedule that instead:

#!/bin/sh
/path/to/job/one &
/path/to/job/two &

In this case I started them in the background to ensure they both run at or nearly at the same time, but depending on your system, your requirements, etc., you will have to adjust this to suit your needs.

Also note that on some systems */2 doesn't actually mean only times divisible by 2, but reoccur in steps equal to 2. So, if the job time matches a 1 when the job is initially started, it may run then every odd minute, as you're seeing, rather than every even one. You could try something like:

00-58/2

to force the job to start on even instances only, but whether and how this works is really dependent on your system, your version of the cron daemon, etc. To get more detailed help, you would have to supply this information :)

share|improve this answer
add comment

No, the crontab is scanned periodically by cron to update its schedule. The schedules on each line of the crontab are interleaved into a global schedule. If you have a line 1,3,5,7,9 and a line 2,4,6,8,10 you will get things happening at 1,2,3,4,5,6,7,8,9,10.

From Wikipedia The algorithm used by the (classic) cron is as follows:

  1. On start-up, look for a file named .crontab in the home directories of all account holders.
  2. For each crontab file found, determine the next time in the future that each command is to be run.
  3. Place those commands on the Franta-Maly event list (IIRC this is an efficient priority queue structure) with their corresponding time and their "five field" time specifier.
  4. Enter main loop:

    1. Examine the task entry at the head of the queue, compute how far in the future it is to be run.
    2. Sleep for that period of time.
    3. On awakening and after verifying the correct time, execute the task at the head of the queue (in background) with the privileges of the user who created it.
    4. Determine the next time in the future to run this command and place it back on the event list at that time value.

If you get a copy of the vixie cron (which is probably what you're using) you can probably look in the source code. I would guess that you're getting the reports offset due to rounding errors in the calculation (sleeps for a minimum of one minute).

share|improve this answer
add comment

Another option, aside from creating a third (external) script, would be to combine both reports into a single command line and put that into cron:

*/2 * * * *  /path/to/job1 ; /path/to/job2
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.