Cron / Crontab commands

Cron reference taken from the cronie 1.7.2 manual pages crontab(1), crontab(5) and cron(8): the crontab command, the five time fields with their operators and nicknames, schedule lines printed in the manual, crontab variables, the system crontab files and the main crond options. A schedule line is minute, hour, day of month, month, day of week, then the command. Extensions such as the random range 6~15 (cronie 1.6.0) and crontab -T FILE (1.5.6) need a recent cronie.

80 commands in 8 sections. Checked row by row against crontab(1) manual page, cronie 1.7.2 (man7.org rendering of the cronie Git repository; text matches the cronie 1.7.2 release) on . Edited by Jay.

Editing and listing crontabs 15

The automatic backup on edit and removal started with cronie 1.7.0.

CommandWhat it does
crontab -eEdit your crontabOpens it in the editor named by VISUAL or EDITOR and installs the result when you leave the editor.
crontab -lList your crontabPrints the table; on a terminal the listing is colourised unless NO_COLOR is set.
crontab -rRemove your crontabDeletes the whole table without asking first.
crontab -r -iRemove with confirmationAsks for a y or Y answer before the table is deleted.
crontab FILEInstall 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 FILETest a file's syntaxChecks the file but leaves the installed table alone, and stops at the first problem found.
crontab -u USER -eEdit 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 -lList another user's crontabShows the table of the named account instead of your own.
crontab -u USER FILEInstall a file for a userLoads the file as the named account's table.
crontab -e -sEdit with SELinux level addedWrites the current SELinux context into the file as an MLS_LEVEL line before the editor opens.
crontab -n HOSTNAMESet the cluster hostPicks the machine that executes the tables of a shared spool directory; crond must have been started with -c.
crontab -cShow the cluster hostPrints the machine that was selected with -n for the shared spool directory.
crontab -VPrint the version
$XDG_CACHE_HOME/crontab/crontab.bakBackup 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

CommandWhat it does
0-59Minute field valuesFirst of the five fields that open every schedule line.
0-23Hour field valuesSecond field, counted on the 24-hour clock with 0 for midnight.
1-31Day-of-month field valuesThird field; when the weekday field is restricted too, a match on either one runs the job.
1-12Month field valuesFourth field; three-letter month names are accepted in place of numbers.
0-7Day-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.

CommandWhat it does
*Every value of a fieldShort for first-last, the whole allowed range of the field it sits in.
8-11Inclusive rangeCovers both end values, so in the hour field it runs at 8, 9, 10 and 11.
1,2,5,9List of valuesCommas join single numbers; the field matches each of them.
0-4,8-12List of rangesRanges can be members of a list, here 0 to 4 together with 8 to 12.
0-23/2Step through a rangeTakes every second value of the range, so the hour field matches 0, 2, 4 and so on.
*/2Step through the whole fieldIn the hour field this starts the command once in two hours.
*/23Step 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~15Random 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,friList of day namesDays are written as three-letter abbreviations, in upper or lower case.
jan-marRange 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

CommandWhat it does
@rebootRun once after rebootThe only nickname without a five-field equivalent.
@yearlyRun once a yearEquals 0 0 1 1 *: midnight on 1 January.
@annuallyYearly run, alternative nameSame schedule as @yearly, 0 0 1 1 *.
@monthlyRun once a monthEquals 0 0 1 * *: midnight on the first day of each month.
@weeklyRun once a weekEquals 0 0 * * 0: midnight at the start of Sunday.
@dailyRun once a dayEquals 0 0 * * *: every day at midnight.
@hourlyRun once an hourEquals 0 * * * *: minute 0 of every hour.

Example schedules from the manual 12

CommandWhat 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-5Weekdays 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 * * sunSundays at 04:05Uses a day name in the fifth field in place of 0 or 7.
30 4 1,15 * 51st, 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 * * 0Sundays 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

CommandWhat it does
NAME = VALUESet a crontab variableSpaces around the equals sign are optional; quote the value to keep leading or trailing blanks.
SHELL=/bin/shChoose the job shellCommands run under /bin/sh by default whatever the passwd entry says; set SHELL to use another shell.
MAILTO=paulMail 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=JapanSet the table's time zoneSchedule times are read in that zone, while log entries keep the daemon's local time.
RANDOM_DELAYRandom start delayIts value is the maximum delay in minutes; the random factor is fixed when the daemon starts.
HOMEOwner's home directoryComes from the owner's passwd entry; a setting in the crontab replaces it.
LOGNAMEOwner's login nameRead from the passwd entry as well, but a crontab cannot change it.
MLS_LEVEL=SystemHighSELinux level for jobsJobs run in the named SELinux security context instead of the creating user's default one.
CONTENT_TYPEMail Content-Type headerReplaces the default text/plain header when job output uses another character encoding.
CONTENT_TRANSFER_ENCODINGMail 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.

CommandWhat it does
/etc/crontabSystem 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/fileSystem job with a user fieldIn /etc/crontab and cron.d files the sixth word names the account that runs the command.
/var/spool/cronPer-user crontab directoryHolds the tables that crontab writes, in files named after the accounts.
/etc/cron.allowUsers allowed to use crontabWhen this file exists, only the users listed in it may run crontab.
/etc/cron.denyUsers barred from crontabRead only when cron.allow is missing; with neither file, only the superuser may use crontab.

Running the cron daemon 9

CommandWhat it does
systemctl start crond.serviceStart the daemon with systemdApplies where systemd is enabled; the unit file is installed as /lib/systemd/system/crond.service.
crond -nRun in the foregroundMeant for starting it straight from init; pam_loginuid must then be off in /etc/pam.d/crond.
crond -fForeground, alternative flagDoes the same as -n and matches the flag of other cron daemons.
crond -sSend job output to syslogFor systems that have no sendmail or have mail switched off.
crond -m offDisable mail from jobsAny other argument to -m is a shell command that receives each message in place of sendmail.
crond -PInherit PATH from environmentStops crond from setting PATH itself, so jobs see the value the daemon was started with.
crond -pAccept any user crontabLifts the requirement that a crontab be a regular file nobody but its owner can write to.
crond -cEnable clustering supportOnly the host named in /var/spool/cron/.cron.hostname then runs the shared user crontabs.
crond -iDisable inotifyLeaves the daemon checking crontab modification times once a minute.

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

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.