17

I had a look though the cron man but didn't find anything that helped :(

Anyone know?

Mint
  • 476
  • 2
  • 9
  • 23

3 Answers3

21

I'm giving an alternative answer here even though Trevor is correct.

The cron @weekly keyword does exactly as he mentioned. However, most distributions use run-parts to run their own scheduled crontab files (on an hourly, daily, weekly and monthly basis) which do not make use of cron's keywords.

E.g., Ubuntu has an /etc/cron.weekly which contains a separate file for each cronjob.

This is generally defined in /etc/crontab

Ubuntu's karmic 9.10 release has the following in /etc/crontab

17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

So the weekly crontab in Ubuntu is run at 6.47am on Sunday

Note: when looking for manpages for crontab implementations, you want to use man 5 crontab instead of just man crontab. The latter will only give you the syntax for the crontab command. The former gives you crontab implementation details.

amc
  • 105
  • 5
Philip Reynolds
  • 9,799
  • 1
  • 34
  • 33
  • A bit nit picky here, section 5 in man pages are for File Formats and Conventions, so not quite implementation. It specifies the file format and often has examples – Steve Buzonas Jan 28 '15 at 00:24
  • 7 is Sunday? I thought 0 was Sunday? (And if it started at 1, I'd think 1 was Sunday.) – felwithe Jan 12 '18 at 15:01
  • @felwithe Sunday is both 0 and 7, so you can pick whichever one best fits the first day of the week in your locale (Monday vs. Sunday). – László van den Hoek Jul 18 '18 at 08:23
  • 1
    I think this is incorrect, so help me understand: `So the weekly crontab in Ubuntu is run at 6.47am on Sunday`. This is incorrect because the `test -x /usr/sbin/anacron ||` part of the command says to ONLY run the stuff to the right of `||` if "/usr/sbin/anacron" is NOT executable or does not exist, yet it does, so the `run-parts` will never actually be run by `cron`! Rather, `anacron` must be handling it, right? Aanacron's table in "/etc/anacrontab" shows this for weekly: `7 10 cron.weekly run-parts --report /etc/cron.weekly`, which means the weekly job is run every 7th day w/a 10 min delay. – Gabriel Staples Jun 17 '19 at 06:35
  • But...I don't know when anacron actually runs or how it determines that. Do you? – Gabriel Staples Jun 17 '19 at 06:35
  • The response, albeit correct as it stands, does NOT respond to the OP's question: it was "what does @weekly mean in crontab" rather than "how to run a weekly job". Nevertheless, cron.{hourly,daily,weekly} folders are used in many distributions, although specific ways of launching the scripts vary. For example in case of OpenSuse the run-crons script is launched every full quarter to run the scripts in the said directories only if enough time passed since the last run. Consequently, the time of running weekly script is somewhat random and may vary over time, especially on the desktop boxes. – Jasio Nov 20 '21 at 13:34
12

@weekly is the equivalent to: 0 0 * * 0

So it'll run at 00:00 on the Sunday.

Trevor Tippins
  • 221
  • 1
  • 2
5

The answer lies in the manpage for the crontab itself, (man 5 crontab):

These special time specification "nicknames" are supported, which replace the 5 initial time and date fields, and are prefixed by the '@' character:

@reboot    :    Run once after reboot.
@yearly    :    Run once a year, ie.  "0 0 1 1 *".
@annually  :    Run once a year, ie.  "0 0 1 1 *".
@monthly   :    Run once a month, ie. "0 0 1 * *".
@weekly    :    Run once a week, ie.  "0 0 * * 0".
@daily     :    Run once a day, ie.   "0 0 * * *".
@hourly    :    Run once an hour, ie. "0 * * * *".

So, it's 0 0 * * 0, which is midnight on Sundays.

(i.e. minute 0 of hour 0 on weekday 0, which is Sunday.)

mwfearnley
  • 816
  • 1
  • 11
  • 22
  • Note: originally I implied `* *` meant "any day of any month", but in reality it means "ignore the day/month fields". If they were filled in, it would run if EITHER the date OR the weekday matched (not only when BOTH the date AND the weekday matched, as might be expected). – mwfearnley Jan 21 '23 at 23:13