Skip to content

crontab Command Cheat Sheet

crontab (cron table) is the command used to install, deinstall, or list the tables used to drive the cron daemon. The cron daemon checks time-based jobs and executes them in the background.


Synopsis

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

Basic Operations

Edit Crontab

Opens your crontab file in the default editor (vi, nano, etc.).

crontab -e

List Crontab

Displays the current crontab content to standard output.

crontab -l

Remove Crontab

Deletes your current crontab file (be careful!).

crontab -r

Prompt Before Removal (-i)

Safely remove with confirmation.

crontab -i -r

Cron Syntax

A standard cron line consists of 5 time-and-date fields, followed by the command.

# Minute   Hour   Day of Month   Month   Day of Week   Command
# (0-59)  (0-23)     (1-31)      (1-12)     (0-7)     (Command to run)
  *        *          *           *          *        /path/to/command

Field Values

Field Allowed Values Special Characters
Minute 0-59 * , - /
Hour 0-23 * , - /
Day of Month 1-31 * , - / L W
Month 1-12 or JAN-DEC * , - /
Day of Week 0-7 or SUN-SAT * , - / L #

(Note: 0 and 7 are both Sunday).


Special Characters

  • * : Every value (every minute, every hour, etc.)
  • , : Value list separator (1,3,5 = 1st, 3rd, 5th)
  • - : Range of values (1-5 = 1, 2, 3, 4, 5)
  • / : Step values (*/5 = every 5th unit)

Common Examples

Every Minute

* * * * * /path/to/script.sh

Every Hour at Minute 0

0 * * * * /path/to/hourly_job.sh

Every Day at Midnight

0 0 * * * /path/to/daily_backup.sh

Every Monday at 5:30 AM

30 5 * * 1 /path/to/weekly_report.sh

Every 15 Minutes

*/15 * * * * /path/to/check_status.sh

Specific Months (e.g., January and June)

0 0 1 1,6 * /path/to/semi_annual.sh

Special Strings (Shortcuts)

Instead of the 5 fields, you can use these shortcuts (if supported by your cron daemon, which Vixie/Paul Vixie cron usually does).

String Equivalent Description
@reboot N/A Run once, at startup
@yearly 0 0 1 1 * Run once a year
@annually 0 0 1 1 * Same as @yearly
@monthly 0 0 1 * * Run once a month
@weekly 0 0 * * 0 Run once a week
@daily 0 0 * * * Run once a day
@midnight 0 0 * * * Same as @daily
@hourly 0 * * * * Run once an hour

Example: Run Script on Boot

@reboot /path/to/startup_script.sh

Environment Variables

Cron runs with a very minimal environment. It does not load your .bashrc or .profile.

Setting PATH

Always define PATH at the top of your crontab to ensure commands are found.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SHELL=/bin/bash
MAILTO=user@example.com

* * * * * my_script.sh

MAILTO

If MAILTO is defined, cron sends the output (stdout/stderr) of jobs to that email address. If empty (MAILTO=""), no email is sent.


Logging and Troubleshooting

Redirect Output

Since you can't see the terminal output of a cron job, redirect it to a log file.

# Standard Output + Standard Error to log file
0 5 * * * /path/backup.sh > /var/log/backup.log 2>&1

# Ignore Output (discard stdout and stderr)
0 5 * * * /path/cleanup.sh > /dev/null 2>&1

Check Cron Logs

On Ubuntu/Debian:

grep CRON /var/log/syslog

On CentOS/RHEL:

tail -f /var/log/cron

Common "Silent Failure" Causes

  1. Permissions: Does the script have +x?
  2. Paths: Are you using absolute paths (/usr/bin/python3) instead of just python3?
  3. Environment: Does the script rely on variables defined in your shell profile?
  4. Newline: Does the crontab file end with a newline? (Some older crons fail without it).

System-Wide Cron

User crontabs are stored in /var/spool/cron/crontabs/. The system also has /etc/crontab and /etc/cron.d/.

Format of /etc/crontab: Includes a user field before the command.

# m h dom mon dow user  command
17 * * * * root    cd / && run-parts --report /etc/cron.hourly
25 6 * * * root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

Managing Other Users

If you have root privileges (sudo), you can manage other users' crontabs.

# Edit alice's crontab
sudo crontab -u alice -e

# List bob's crontab
sudo crontab -u bob -l

Security (Cron Access Control)

You can allow or deny users from using crontab.

  • /etc/cron.allow: If exists, ONLY users listed here can use crontab.
  • /etc/cron.deny: If cron.allow doesn't exist, users listed here CANNOT use crontab.
  • If neither exists, usually only root can use it (depending on distro).

Exit Status

Code Meaning
0 Success
1 Error (often syntax)

Tools