Debian Cron & AnacronBy Dag J, on December 30th, 2016
Cron, as supplied in Debian, has two purposes
- To run system jobs on a daily/weekly/monthly basis
- To allow users to setup their own schedules
Except for the first one which is special (manpage: In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab), these directories allow scheduling of system-wide jobs in a coarse manner. Any script which is executable and placed inside them will run at the frequency which its name suggests. By default you can see in /etc/crontab that it checks if anacron exists before running the intermittent jobs daily, weekly, monthly. If you need exact timing on a continous system you should use cron as anacron is for non-continuous systems and doesn't really care about the exact time the jobs run, as long as they run within the intermittent period (day, month, weekly). Hourly seems to still be controlled by cron. I advise using cron for continuous systems and exact timing.
/etc/crontab and the files in /etc/cron.d must be owned by root, and must not be group- or other-writable. The files under /etc/cron.d or the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly may also be symlinks, provided that both the symlink and the file it points to are owned by root. Cron wakes up every minute, examining all stored crontabs, checking each command to see if it should be run in the current minute. When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists).
If you wish to stop output from being e-mailed to you, just redirect it:
0 * * * * /bin/ls > /dev/null 2&>1
The normal manner which people use cron is via the crontab command. This allows you to view or edit your crontab file, which is a per-user file containing entries describing commands to execute and the time(s) to execute them. To display your file you run the following command:
$ crontab -l ^ (edit with -e)
Root can view any users crontab file by adding "-u username", for example:
# crontab -u username -l
The format of these files is fairly simple to understand. Each line is a collection of six fields separated by spaces.
The fields are:
1. The number of minutes after the hour (0 to 59) 2. The hour in military time (24 hour) format (0 to 23) 3. The day of the month (1 to 31) 4. The month (1 to 12) 5. The day of the week(0 or 7 is Sun, or use name) 6. The command to run More graphically they would look like this: * * * * * Command to be executed - - - - - | | | | | | | | | +----- Day of week (0-7) | | | +------- Month (1 - 12) | | +--------- Day of month (1 - 31) | +----------- Hour (0 - 23) +------------- Min (0 - 59)
# Run the `something` command every hour on the hour 0 * * * * /sbin/something # Run the `nightly` command at ten minutes past midnight every day 10 0 * * * /bin/nightly # Run the `monday` command every monday at 2 AM 0 2 * * 1 /usr/local/bin/monday One last tip: If you want to run something very regularly you can use an alternate syntax: Instead of using only single numbers you can use ranges or sets. A range of numbers indicates that every item in that range will be matched, if you use the following line you'll run a command at 1AM, 2AM, 3AM, and 4AM: # Use a range of hours matching 1, 2, 3 and 4AM * 1-4 * * * /bin/some-hourly A set is similar, consisting of a collection of numbers seperated by commas, each item in the list will be matched. The previous example would look like this using sets: # Use a set of hours matching 1, 2, 3 and 4AM * 1,2,3,4 * * * /bin/some-hourly