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.

Suppose I have several cron jobs set up to run at the same time: do they execute in parallel or just one after another?

(My case is Debian squeeze with cronjobs put inside /etc/cron.d/mycronjobs).

share|improve this question
add comment

4 Answers

The tasks listed in cron will run in parallel, just as processes usually do. Theres no way of being sure which will start first, and no way in cron to make sure task A has complete before task B starts.

share|improve this answer
    
You can make sure task A has complete before task B starts by listing them on the same line and separating them with a semicolon. –  Nicholas May 7 '12 at 21:08
    
That doesnt mean task A has completed successfully. Perhaps you could use a &&, but anything like this should be carried out by a script that is called. –  Sirch May 8 '12 at 8:42
    
You can use any operands the bash shell supports. Using &&,||, or ; in cron is common practice in my experience. –  Nicholas May 8 '12 at 15:25
add comment

For completeness, jobs in e.g. /etc/cron.{hourly,daily,weekly,monthly} are run sequentially. The run-parts script loops over all files in this directory.

02 4 * * * root run-parts /etc/cron.daily

So you can use that in combination with a naming convention (similar to the numbering in /etc/init.d/rc*.d for example) to make sure jobs run sequentially.

share|improve this answer
    
What if I have several cron jobs within the same file, set to run at the same time? –  Gabriele May 3 '12 at 10:38
    
I'm not sure what you mean by "within the same file". But if you mean that you have several lines with the same time specification (like the 0 * * * * you posted earlier) then as other have posted there is no guarantee on the order they will run in. They may run in parallel on a multi-core system, AFAIK the order in which they run may even be different each day. The naming convention I mentioned in combination with a run-parts or the use of a master script that calls the actual backup scripts is the only way to ensure scripts are executed in a particular order. –  Bram May 7 '12 at 6:43
    
Ok then, since I need to be sure my scripts will run one after another, I think I'll go for a "master script" triggered by a cron job, and call any scripts from within the main script. –  Gabriele May 7 '12 at 11:08
add comment

Cron is a daemon (service) that runs continuously; however, it reads crontabs once a minute.

The exact sequence in which jobs are executed will depend on the implementation of your systems' crond.

The loose files that some distributions put inside /etc/cron.d/ are scanned for their cron timer settings, since these files follow the normal crontab(5) syntax.

What order the individual jobs are executed in depends on the schedule you set for them, obviously.

share|improve this answer
    
What if the schedule is set like this: ---- 0 * * * * root sh /var/opt/backup_scripts/backup1 - 0 * * * * root sh /var/opt/backup_scripts/backup2 ---- Will they run sequentially, i.e. backup1 and, when finished, backup2? –  Gabriele May 3 '12 at 11:05
    
The exact sequence in which jobs are executed will depend on the implementation of your systems' crond. man cron for details. –  adaptr May 3 '12 at 14:10
    
man cron states nothing about that (at least in Debian). –  Gabriele May 7 '12 at 11:04
add comment

They will run in parallel. You can use the following methods to run the processes sequentially.

# Use a semicolon to run command2 after command1 has completed
02 4 * * * /path/to/command1 ; /path/to/command2

# Use two ampersands to run command2 after command1 has completed successfully.
02 4 * * * /path/to/command1 && /path/to/command2

# Use two vertical rules to run command2 after command1 has completed unsuccessfully.
02 4 * * * /path/to/command1 || /path/to/command2
share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.