Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want two jobs to run sometime every day, serially, in exactly the order I specify. Will this crontab reliably do what I want?

@daily job1
@daily job2

I'm assuming they run one after the other, but I was unable to find the answer by searching the Web or from any of these manpages: cron(1), crontab(1), crontab(5).

The crontab above obviously won't do what I want if cron runs things scheduled with @daily in parallel or in an unpredictable order.

I know I can simply make one shell script to fire them off in order, I'm just curious how cron is supposed to work (and I'm too lazy to gather test data or read the source code).

Cron is provided by the cron package. OS is Ubuntu 10.04 LTS (server).

share|improve this question
    
Do you happen to know which cron is being provided by the cron package? Almost all cron daemons I know will handle this in order, and synchronously, usually first parsing each file in alphabetical order (although some do it by amount of time between runs in ascending order), and then the jobs inside in line order. –  Chris Down Dec 5 '11 at 21:40
2  
The fact that it is under-specified usually means it is left up to the implementation, and it's usually best not to depend on such behavior. It's probably cleaner and better organized to put a sequence of jobs where the sequence matters in its own script instead of as separate crontab entries. –  jw013 Dec 5 '11 at 22:24
    
@ChrisDown: Vixie Cron, I think. packages.ubuntu.com/lucid/cron –  Adam Monsen Dec 5 '11 at 22:26
    
I'd suggest if you really want to run them in order, make one execute one minute later. –  Kevin Dec 6 '11 at 0:24
    
@jw013 and Kevin: agreed, but that's not my question. –  Adam Monsen Dec 6 '11 at 0:39
add comment

3 Answers 3

up vote 7 down vote accepted

After a quick glance at the source (in Debian squeeze, which I think is the same version), it does look like entries within a given file and with the same times are executed in order. For this purpose, @daily and 0 0 * * * are identical (in fact @daily is identical to 0 0 * * * in this cron).

I would not rely on this across the board. It's possible that one day someone will decide that cron should run jobs in parallel, to take advantage of these 32-core CPUs that have 31 cores running idle. This might be done when implementing this 20-year old todo item encountered in the cron source:

All of these should be flagged and load-limited; i.e., instead of @hourly meaning "0 * * * *" it should mean "close to the front of every hour but not 'til the system load is low". (…) (vix, jan90)

It's very easy to write @daily job1; job2 here. If it's important that the jobs execute in order, make it a direct consequence of what you write.

Additionally, making the order explicit removes the risk that a future administrator will reorder the lines thinking that it won't matter.

share|improve this answer
add comment

http://ss64.com/osx/crontab.html (as well as other references) says that @daily is equivalent to

0 0 * * *

which says to run at midnight. I would expect two lines of this sort to both be launched as close to midnight as possible, with no guarantee as to which would run first. I agree with jw013's suggestion in his comment:

The fact that it is under-specified usually means it is left up to the implementation, and it's usually best not to depend on such behavior. It's probably cleaner and better organized to put a sequence of jobs where the sequence matters in its own script instead of as separate crontab entries.

share|improve this answer
    
This helps, thanks. I agree @jw013's suggestion is valid, but that's not the question I asked. I guess I'll have to consult the actual source for the definitive answer, since this is probably, indeed, implementation-specific. –  Adam Monsen Dec 6 '11 at 0:38
add comment

@Giles comment on using a single script when it is import to run things in order is appropriate.

cron.daily files do not specify run times. It has been my experience that they do run serially. This makes sense as many background jobs that might run on a laptop or other system

When times are specified all jobs scheduled for the current minute are run. It is a good idea to use different times.

If you have jobs that are mutually exclusive, it is common to use a lock file to determine if the other job is running.

share|improve this answer
    
@daily is slightly different than /etc/cron.daily. The former is an alternate syntax for use in a crontab. Also have a look at run-parts. My system uses run-parts to run programs in /etc/cron.daily. According to the run-parts(8) manpage, "Files are run in the lexical sort order of their names...". –  Adam Monsen Dec 6 '11 at 4:24
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.