Editing and listing crontabs 15
The automatic backup on edit and removal started with cronie 1.7.0.
| Command | What it does |
|---|---|
crontab -e | Edit your crontabOpens it in the editor named by VISUAL or EDITOR and installs the result when you leave the editor. |
crontab -l | List your crontabPrints the table; on a terminal the listing is colourised unless NO_COLOR is set. |
crontab -r | Remove your crontabDeletes the whole table without asking first. |
crontab -r -i | Remove with confirmationAsks for a y or Y answer before the table is deleted. |
crontab FILE | Install a crontab from a fileThe contents of the file become your crontab, replacing the one installed before. |
crontab - | Install from standard inputThe hyphen is required when standard input is a terminal; what is read replaces your table. |
crontab -T FILE | Test a file's syntaxChecks the file but leaves the installed table alone, and stops at the first problem found. |
crontab -u USER -e | Edit another user's crontabWithout -u the command works on the table of whoever runs it; a missing one is created on first use. |
crontab -u USER -l | List another user's crontabShows the table of the named account instead of your own. |
crontab -u USER FILE | Install a file for a userLoads the file as the named account's table. |
crontab -e -s | Edit with SELinux level addedWrites the current SELinux context into the file as an MLS_LEVEL line before the editor opens. |
crontab -n HOSTNAME | Set the cluster hostPicks the machine that executes the tables of a shared spool directory; crond must have been started with -c. |
crontab -c | Show the cluster hostPrints the machine that was selected with -n for the shared spool directory. |
crontab -V | Print the version |
$XDG_CACHE_HOME/crontab/crontab.bak | Backup of last crontabWritten whenever a table is edited or deleted; $HOME/.cache is used when XDG_CACHE_HOME is unset, and -u adds the user name. |
Time fields 5
| Command | What it does |
|---|---|
0-59 | Minute field valuesFirst of the five fields that open every schedule line. |
0-23 | Hour field valuesSecond field, counted on the 24-hour clock with 0 for midnight. |
1-31 | Day-of-month field valuesThird field; when the weekday field is restricted too, a match on either one runs the job. |
1-12 | Month field valuesFourth field; three-letter month names are accepted in place of numbers. |
0-7 | Day-of-week field valuesFifth field; both ends of the scale mean Sunday, and three-letter day names work too. |
Operators and special characters 14
The ~ random-range operator exists from cronie 1.6.0 onwards.
| Command | What it does |
|---|---|
| * | Every value of a fieldShort for first-last, the whole allowed range of the field it sits in. |
8-11 | Inclusive rangeCovers both end values, so in the hour field it runs at 8, 9, 10 and 11. |
| 1,2,5,9 | List of valuesCommas join single numbers; the field matches each of them. |
0-4,8-12 | List of rangesRanges can be members of a list, here 0 to 4 together with 8 to 12. |
0-23/2 | Step through a rangeTakes every second value of the range, so the hour field matches 0, 2, 4 and so on. |
| */2 | Step through the whole fieldIn the hour field this starts the command once in two hours. |
*/23 | Step that does not wrapIn the hour field it fires at 0 and at 23 each day, not once in 23 hours, because steps never leave their field. |
6~15 | Random value in a rangeIn the minute field cron chooses one minute from 6 to 15, once, when it parses the crontab. |
| ~ | Random value, whole fieldWith both numbers left out the choice covers the full field, 0 to 59 for minutes. |
mon,wed,fri | List of day namesDays are written as three-letter abbreviations, in upper or lower case. |
jan-mar | Range of month namesOnly the month and weekday fields take names, in ranges as well as lists. |
| # | Comment lineOnly a whole line can be a comment; text after a command or a variable setting is not treated as one. |
| % | Newline and standard inputAn unescaped percent ends the command; what follows is fed to it as input, so write a backslash before a literal one. |
| - | Skip the syslog entryPlaced at the very start of an entry in root's crontab, it stops cron logging each run of that command. |
Special strings 7
| Command | What it does |
|---|---|
@reboot | Run once after rebootThe only nickname without a five-field equivalent. |
@yearly | Run once a yearEquals 0 0 1 1 *: midnight on 1 January. |
@annually | Yearly run, alternative nameSame schedule as @yearly, 0 0 1 1 *. |
@monthly | Run once a monthEquals 0 0 1 * *: midnight on the first day of each month. |
@weekly | Run once a weekEquals 0 0 * * 0: midnight at the start of Sunday. |
@daily | Run once a dayEquals 0 0 * * *: every day at midnight. |
@hourly | Run once an hourEquals 0 * * * *: minute 0 of every hour. |
Example schedules from the manual 12
| Command | What it does |
|---|---|
* * * * * | Every minuteFive asterisks leave every field unrestricted, so the line matches each minute cron checks. |
5 0 * * * | Daily at 00:05The manual's line sends the job's output and errors to a file with >> and 2>&1. |
15 14 1 * * | Monthly on the 1st at 14:15With no redirection, whatever the job prints is mailed to the MAILTO address set above it. |
0 22 * * 1-5 | Weekdays at 22:00The range 1-5 in the fifth field selects Monday to Friday. |
23 0-23/2 * * * | Every two hours at minute 23Fires at 00:23, 02:23, 04:23 and so on through the day. |
5 4 * * sun | Sundays at 04:05Uses a day name in the fifth field in place of 0 or 7. |
30 4 1,15 * 5 | 1st, 15th and every FridayRuns at 04:30; both day fields are restricted, so a match on either one is enough. |
0 0 1 1 * | Yearly, 1 January at midnightThe five-field form of @yearly and @annually. |
0 0 1 * * | Monthly on the 1st, midnightThe line that the nickname @monthly stands for. |
0 0 * * 0 | Sundays at midnightThe line behind @weekly; 7 in the last field would also mean Sunday. |
0 0 * * * | Daily at midnightThe full line that the nickname @daily abbreviates. |
0 * * * * | Hourly, on the hourMinute 0 of every hour, the same schedule as @hourly. |
Environment variables 12
| Command | What it does |
|---|---|
NAME = VALUE | Set a crontab variableSpaces around the equals sign are optional; quote the value to keep leading or trailing blanks. |
SHELL=/bin/sh | Choose the job shellCommands run under /bin/sh by default whatever the passwd entry says; set SHELL to use another shell. |
MAILTO=paul | Mail job output to a userWithout MAILTO the output goes to the owner of the crontab. |
MAILTO="" | Turn off job mailAn empty value stops mail for the jobs in that crontab. |
[email protected] | Set the envelope sender$USER expands to the account name; without MAILFROM the sender is the user running the job. |
CRON_TZ=Japan | Set the table's time zoneSchedule times are read in that zone, while log entries keep the daemon's local time. |
RANDOM_DELAY | Random start delayIts value is the maximum delay in minutes; the random factor is fixed when the daemon starts. |
HOME | Owner's home directoryComes from the owner's passwd entry; a setting in the crontab replaces it. |
LOGNAME | Owner's login nameRead from the passwd entry as well, but a crontab cannot change it. |
MLS_LEVEL=SystemHigh | SELinux level for jobsJobs run in the named SELinux security context instead of the creating user's default one. |
CONTENT_TYPE | Mail Content-Type headerReplaces the default text/plain header when job output uses another character encoding. |
CONTENT_TRANSFER_ENCODING | Mail transfer encoding headerSets the mail header of the same name on messages that carry job output. |
System crontab files and access control 6
cron.allow and cron.deny only limit who may run the crontab command; jobs in an existing crontab keep running until it is removed.
| Command | What it does |
|---|---|
/etc/crontab | System crontab fileEmpty by default today; the daily, weekly and monthly jobs it used to hold go through anacron. |
/etc/cron.d/ | Directory of system jobsIts files use the system format, with an account name between the time fields and the command. |
* * * * * root touch /tmp/file | System job with a user fieldIn /etc/crontab and cron.d files the sixth word names the account that runs the command. |
/var/spool/cron | Per-user crontab directoryHolds the tables that crontab writes, in files named after the accounts. |
/etc/cron.allow | Users allowed to use crontabWhen this file exists, only the users listed in it may run crontab. |
/etc/cron.deny | Users barred from crontabRead only when cron.allow is missing; with neither file, only the superuser may use crontab. |
Running the cron daemon 9
| Command | What it does |
|---|---|
systemctl start crond.service | Start the daemon with systemdApplies where systemd is enabled; the unit file is installed as /lib/systemd/system/crond.service. |
crond -n | Run in the foregroundMeant for starting it straight from init; pam_loginuid must then be off in /etc/pam.d/crond. |
crond -f | Foreground, alternative flagDoes the same as -n and matches the flag of other cron daemons. |
crond -s | Send job output to syslogFor systems that have no sendmail or have mail switched off. |
crond -m off | Disable mail from jobsAny other argument to -m is a shell command that receives each message in place of sendmail. |
crond -P | Inherit PATH from environmentStops crond from setting PATH itself, so jobs see the value the daemon was started with. |
crond -p | Accept any user crontabLifts the requirement that a crontab be a regular file nobody but its owner can write to. |
crond -c | Enable clustering supportOnly the host named in /var/spool/cron/.cron.hostname then runs the shared user crontabs. |
crond -i | Disable inotifyLeaves the daemon checking crontab modification times once a minute. |
No row on this page matches that filter.
Notes
Editing safely
crontab -e opens your crontab in the editor named by VISUAL or EDITOR and installs it when you leave the editor; crontab -l prints the current one. crontab -r removes the whole crontab with no confirmation — it sits one key away from -e on the keyboard, which is why crontab -r -i exists to add a prompt. Since cronie 1.7.0 every edit or removal also leaves the previous version in $XDG_CACHE_HOME/crontab/crontab.bak (under $HOME/.cache when that variable is unset). crontab -T FILE syntax-checks a file without installing it, stopping at the first problem, and crontab -u USER -e edits another user's schedule.
Reading the five fields
A line is minute, hour, day of month, month, day of week, then the command: * * * * * runs every minute, 5 0 * * * at 00:05 daily, 0 22 * * 1-5 at 22:00 on weekdays, 0 0 * * 0 at midnight on Sundays and 0 0 1 * * at midnight on the first of the month. Day of month and day of week are combined with OR when both are set, which surprises people who write "the first Monday" and get every Monday plus every first; the manual's 30 4 1,15 * 5 runs on the 1st, the 15th and every Friday. Steps stay inside their field, so */23 in the hour field means hours 0 and 23, not every 23 hours. The @ strings are aliases for common lines — @daily, @hourly, @weekly — and @reboot runs once after a reboot, which has no five-field equivalent.
Why the job worked in the shell but not in cron
Cron does not run commands in your login environment: SHELL is /bin/sh whatever your account uses, HOME and LOGNAME come from the passwd entry, and crond sets PATH itself unless it was started with crond -P. Output that is not redirected (>> $HOME/tmp/out 2>&1 in the manual's example) is mailed to the crontab's owner or to the MAILTO address, and goes to syslog when sendmail is not installed. A % in the command is turned into a newline unless escaped with a backslash, and the crontab must end with a newline or cron treats it as broken. Times are in the system time zone unless a line such as CRON_TZ=Japan sets another one for the table.
Questions
How do I run a job every 15 minutes?
*/15 * * * * command. The slash means "every N" within the field's range, here minutes 0, 15, 30 and 45. A step never carries over into the next hour or day, which is why the manual warns that */23 in the hour field runs at hours 0 and 23 only.
What is the difference between @daily and 0 0 * * *?
None — @daily is an alias for exactly that line. @reboot is the only special string with no field equivalent.
Why does my cron job not run?
Check what the manual names: the crontab must end with a newline, an unescaped % cuts the command short, and commands run under /bin/sh with cron's own environment rather than your login shell. Errors arrive by mail to the crontab's owner, or in syslog when sendmail is missing. crontab -l confirms the schedule is installed.
How do I check a crontab for errors before installing it?
crontab -T FILE validates the syntax without installing the file. It stops at the first problem it finds, so run it again after each fix. The option arrived in cronie 1.5.6.
Sources and checks
- crontab(1) manual page, cronie 1.7.2 (man7.org rendering of the cronie Git repository; text matches the cronie 1.7.2 release) The crontab command and its options, the backup file, cron.allow and cron.deny
- crontab(5) manual page, cronie 1.7.2 (man7.org rendering of the cronie Git repository; text matches the cronie 1.7.2 release) Time fields, operators, nicknames, variables and the example crontab lines
- cron(8) manual page, cronie 1.7.2 (man7.org rendering of the cronie Git repository; text matches the cronie 1.7.2 release) System crontab files and the crond daemon options
- cronie NEWS file, release history up to cronie 1.7.2 Release in which an option or operator first appeared
Every row on this page was matched to a line in the documentation above on . A binding that could not be found there is not listed.
2 rows from the earlier version of this page were removed in that check: duplicates, and bindings the documentation does not list or lists differently.
Found a wrong or missing key? Tell us and it gets checked against the source. How we verify describes the process and its limits.