Penguin
Annotated edit history of CronNotes version 10, including all changes. View license author blame.
Rev Author # Line
4 AristotlePagaltzis 1 !!! Introduction
2
3 Scheduling under Linux is provided by the cron(8) daemon. It reads a file called a crontab(5), but you don't necessarily need to edit any cron table directly.
4
5 !!! Running commands at standard intervals
6
8 AristotlePagaltzis 7 On most systems, the easiest thing you can do if you simply want your command to be run once an hour, or once a day, or once a week, etc, is to put a script in the corresponding one of the <tt>/etc/cron.{hourly,daily,weekly,monthly}</tt> etc directories. This is a simple [Shell] script, not a crontab(5) file. On most systems, all the “daily” scripts will be run sometime in the wee hours, such as 4am.
6 GreigMcGill 8
8 AristotlePagaltzis 9 Something to note is that this is usually done by a script called <tt>run-parts</tt> which has strange restrictions on what it considers a valid filename. The Linux Standard Base seems to be to blame for this: [LSB: Cron jobs|http://www.linuxbase.org/spec/book/LSB-generic/LSB-generic/sysinit.html]. DebianLinux' version of <tt>run-parts</tt> is even worse, defaulting to their legacy scheme which only allows ~[A-Za-z0-9_-] chars! This means it will silently ignore any scripts that contain a dot in the name, which is a horrible bug.
4 AristotlePagaltzis 10
11 !!! Running commands at custom times/intervals
12
13 If you need to run commands at custom times or intervals, you need to know the crontab(5) line format described in the ManPage. Read that now, if you don't know about it yet.
14
8 AristotlePagaltzis 15 The main, system wide crontab(5) is <tt>/etc/crontab</tt>, but on most LinuxDistribution~s, there is a <tt>/etc/cron.d</tt> directory where you should put single line crontab files, eg:
4 AristotlePagaltzis 16
8 AristotlePagaltzis 17 <verbatim>
18 # /etc/cron.d/exim: crontab fragment for exim
19 # Run queue every 15 minutes
20 08,23,38,53 * * * * mail if [ -x /usr/sbin/exim -a -f /etc/exim/exim.conf ]; then /usr/sbin/exim -q ; fi
21 </verbatim>
4 AristotlePagaltzis 22
8 AristotlePagaltzis 23 This will be run every 15 minutes (8 past the hour, 23 past the hour etc) on every hour, every day, etc, as user mail. The initial fields are delimited by spaces, up to the the command (<tt>if ~[ ... ] ; then ... ; fi</tt>), which is taken as a whole to the end of the line. (The <tt>if</tt> bit is a sanity check to ensure a runnable [Exim] binary and an <tt>exim.conf</tt> file exist.)
4 AristotlePagaltzis 24
8 AristotlePagaltzis 25 Users can also have their own cron tables, with commands run under their respective [UID]. These should be manipulated using the crontab(1) command. <tt>crontab -e</tt> will launch your configured interactive TextEditor on your user crontab, which generally resides under <tt>/var/spool/cron</tt>.
4 AristotlePagaltzis 26
27 !!! Running commands at standard intervals, revisited, and more
28
5 AristotlePagaltzis 29 With Vixie Cron (used in [*BSD] and some [LinuxDistribution]s such as [Debian] and RedHat) you can use several special keywords instead of a time specification. This [FreeBSD 4.1 manpage | http://www.freebsd.org/cgi/man.cgi?query=crontab&apropos=0&sektion=5&manpath=FreeBSD+4.1-RELEASE&format=html], where this feature first appears, lists the following keywords:
30
7 JohnMcPherson 31 <?plugin OldStyleTable
8 AristotlePagaltzis 32 |^ string |^ meaning
33 | <tt>@reboot</tt> | Run once, at startup.
34 | <tt>@yearly</tt> | Run once a year, <tt>0 0 1 1 *</tt>.
35 | <tt>@annually</tt> | (sames as @yearly)
36 | <tt>@monthly</tt> | Run once a month, <tt>0 0 1 * *</tt>.
37 | <tt>@weekly</tt> | Run once a week, <tt>0 0 * * 0</tt>.
38 | <tt>@daily</tt> | Run once a day, <tt>0 0 * * *</tt>.
39 | <tt>@midnight</tt> | (same as @daily)
40 | <tt>@hourly</tt> | Run once an hour, <tt>0 * * * *</tt>.
7 JohnMcPherson 41 ?>
9 JohnMcPherson 42
5 AristotlePagaltzis 43
8 AristotlePagaltzis 44 <tt>@reboot</tt> is particularly interesting, as cron will run such a command at system startup. This allows regular users to start their own daemons such as fetchmail(1) at boot time.
10 JohnMcPherson 45
46 !!!Troubleshooting
47 !!Command isn't being run properly
48 * cron uses a minimal PATH EnvironmentVariable. Explicitly set the PATH variable if you want anything not in /usr/bin and /bin (+ maybe /usr/local?)
49 * You have a "%" symbol in your command, and cron treats this in a special fashion. From the crontab(5) ManPage:
50 > The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, ala the shell’s trailing "\".