crontab.run

Crontab command explanation

The cron daemon (crond) is a background service on Unix-like operating systems that executes scheduled commands. It reads configuration from crontab (cron table) files, which define when and how often each command should run.

Syntax

The crontab command manages per-user crontab files:

crontab [-u user] [-l | -r | -e | -i] [file]

Options

-e
Edit the current user's crontab in the default editor ($VISUAL or $EDITOR). The new crontab is installed after the editor exits.
-l
Display the current user's crontab to standard output.
-r
Remove the current user's crontab entirely.
-i
Prompt for confirmation before removing (used with -r).
-u user
Operate on the specified user's crontab instead of your own. Requires root privileges.
file
Install the crontab from the given file. Use - for standard input.

Crontab file format

Each line in a crontab file is either an environment variable assignment, a comment (starting with #), or a cron job with this format:

┌───────────── minute (0–59)
│ ┌───────────── hour (0–23)
│ │ ┌───────────── day of month (1–31)
│ │ │ ┌───────────── month (1–12 or JAN–DEC)
│ │ │ │ ┌───────────── day of week (0–6 or SUN–SAT)
│ │ │ │ │
* * * * *  command to execute

Field operators

* (asterisk)
Matches every possible value for the field.
, (comma)
Specifies a list of values. Example: 1,15 in the day-of-month field means the 1st and 15th.
- (hyphen)
Defines an inclusive range. Example: 9-17 in the hour field means every hour from 9 through 17.
/ (slash)
Defines a step. Example: */10 in the minute field means every 10 minutes. Can be combined with a range: 1-30/5.

Special strings

Instead of the five time fields, you can use one of these shorthand strings:

StringEquivalent
@yearly / @annually0 0 1 1 *
@monthly0 0 1 * *
@weekly0 0 * * 0
@daily / @midnight0 0 * * *
@hourly0 * * * *
@rebootRun once at startup

Environment variables

You can set environment variables at the top of a crontab file. The most common ones:

SHELL
The shell used to run commands (default: /bin/sh).
PATH
The search path for commands. Cron's default PATH is minimal (usually /usr/bin:/bin), so always use absolute paths or set PATH explicitly.
MAILTO
Where to send command output. Set to "" to suppress email. By default, output is mailed to the crontab owner.
CRON_TZ
Set the timezone for the crontab (not supported on all systems). Without it, cron uses the system timezone.
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
[email protected]

*/5 * * * * /home/user/scripts/backup.sh

System crontab vs. user crontab

User crontabs (edited with crontab -e) have five time fields plus the command. The system crontab (/etc/crontab) and files in /etc/cron.d/ have an extra field between the time fields and the command: the username the command runs as.

System crontab example:

# /etc/crontab
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m  h  dom mon dow user    command
*/15 *  *   *   *   root    /usr/local/bin/system-check.sh
0    2  *   *   *   backup  /usr/local/bin/nightly-backup.sh

Common pitfalls

% (percent sign)

The percent sign (%) has special meaning in crontab: it is translated to a newline, and everything after the first % is sent as standard input to the command. To use a literal %, escape it as \%.

# Wrong — the % will be interpreted
0 0 * * * echo "Date: $(date +%Y-%m-%d)"

# Correct — escape the %
0 0 * * * echo "Date: $(date +\%Y-\%m-\%d)"

PATH

Cron runs with a minimal PATH. Scripts that rely on commands in /usr/local/bin or other directories should either use absolute paths or set PATH at the top of the crontab.

Day-of-month + day-of-week

When both day-of-month and day-of-week are restricted (not *), cron runs the command when either field matches (OR logic), not when both match. For example, 0 0 1 * 5 runs at midnight on the 1st of every month AND every Friday.

Timezone

Cron uses the system timezone by default. If your server is in UTC but you want jobs in a local timezone, use CRON_TZ (where supported) or convert times manually.

Output and logging

By default, cron mails any output (stdout and stderr) to the crontab owner. To silence a job, redirect output: command > /dev/null 2>&1. To log output, redirect to a file: command >> /var/log/myjob.log 2>&1.

# Discard all output
*/5 * * * * /path/to/script.sh > /dev/null 2>&1

# Log output to a file
*/5 * * * * /path/to/script.sh >> /var/log/myscript.log 2>&1

Practical examples