Are multiple @daily crontab entries processed in order, serially?

29,198

Solution 1

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.

Solution 2

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:
29,198
Adam Monsen
Author by

Adam Monsen

Stoked about FLOSS. Seeking the truth. Kind. Head of Engineering at C-SATS, Inc. SeaGL founder.

Updated on September 18, 2022

Comments

  • Adam Monsen
    Adam Monsen over 1 year

    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).

    • clerksx
      clerksx over 12 years
      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.
    • Adam Monsen
      Adam Monsen over 12 years
      @ChrisDown: Vixie Cron, I think. packages.ubuntu.com/lucid/cron
    • Kevin
      Kevin over 12 years
      I'd suggest if you really want to run them in order, make one execute one minute later.
    • Adam Monsen
      Adam Monsen over 12 years
      @jw013 and Kevin: agreed, but that's not my question.
  • Adam Monsen
    Adam Monsen over 12 years
    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
    Adam Monsen over 12 years
    @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...".