Cron Jobs Explained: Crontab Syntax With Examples

Cron runs commands on a schedule on Linux and macOS servers. This guide explains the crontab format, shows the schedules people need most, and lists the mistakes that make cron jobs fail silently.

Open the tool: Cron Expression Explainer and Generator

Add a cron job

Run crontab -e to open your personal crontab in an editor. Add one line per job: the five schedule fields followed by the command, for example 0 2 * * * /home/me/backup.sh. Save and close the editor, and cron picks up the change automatically.

Use crontab -l to list your jobs. System-wide jobs live in /etc/crontab and /etc/cron.d, where an extra field after the schedule names the user that runs the command.

Read the five fields

From left to right the fields are minute, hour, day of month, month and day of week. A job runs when all fields match the current time. An asterisk matches every value, commas make lists, hyphens make ranges and a slash sets a step.

Write output to a log

Cron has no terminal, so output is mailed to the user or lost. Add >> /var/log/myjob.log 2>&1 at the end of the command to keep a log you can check when something goes wrong.

Why a cron job does not run

  • The PATH in cron is minimal. Use full paths to programs and scripts, such as /usr/bin/python3.
  • The script is not executable. Run chmod +x on it.
  • Environment variables from your shell profile are not loaded. Set them in the crontab or in the script.
  • The server time zone differs from yours. Check with date or timedatectl.
  • A % in the command is turned into a new line. Escape it as \%.

Check the schedule before you deploy

Paste the expression into the cron explainer on this page to see it in plain words and check the next run times. It catches out-of-range values and the day-of-month or day-of-week trap before the job goes live.