OneStopList

Gods and goddesses

How Crontab Works: Syntax, Timing & Examples

Updated November 25, 2026

Crontab is the command-line utility on Unix-like systems that lets you schedule commands or scripts to run automatically at fixed times, dates, or intervals. It works by reading a per-user table of jobs (the crontab file), which the cron daemon checks every minute and executes any job whose schedule matches the current time. This article explains the crontab format, how the cron daemon processes entries, common pitfalls, and practical examples.

Background: What Cron and Crontab Are

Cron is a job scheduler that has been part of Unix systems since the 1970s. The name comes from the Greek word chronos (time). The cron daemon (crond) runs in the background, waking up every minute to check for jobs that need to run. The crontab command is the user interface: it lets you create, edit, list, and remove your own scheduled jobs. Each user on a system can have their own crontab file, stored in a system directory (often /var/spool/cron/crontabs/ or /var/spool/cron/). System-wide cron jobs are typically placed in /etc/crontab or the /etc/cron.d/ directory, which use a slightly extended format.

The Crontab Syntax: Five Time Fields

Each line in a crontab file defines one job. The basic syntax has five time-and-date fields followed by the command to run:

  1. Minute (0–59)
  2. Hour (0–23, 0 = midnight)
  3. Day of month (1–31)
  4. Month (1–12, or names like jan, feb)
  5. Day of week (0–7, where both 0 and 7 mean Sunday, or names like sun, mon)
  6. Command – the shell command or script to execute

An asterisk (*) in a field means “every” – for example, * * * * * runs every minute. You can also use:

  • Comma to list multiple values: 0,30 * * * * runs at minute 0 and 30 of every hour.
  • Hyphen for a range: 9-17 means hours 9 through 17.
  • Step with a slash: */15 in the minute field means every 15 minutes.
  • Names for months and days: jan, mar or mon-fri.

Here are concrete examples:

  • 30 2 * * * /usr/bin/backup.sh – runs at 2:30 AM every day.
  • 0 9 * * 1-5 /usr/bin/report – runs at 9:00 AM Monday through Friday.
  • */10 * * * * /usr/bin/check_status – runs every 10 minutes.
  • 0 0 1 * * /usr/bin/monthly_cleanup – runs at midnight on the first day of every month.

How the Cron Daemon Interprets the Schedule

The cron daemon reads all crontab files into memory at startup and then checks them every minute. It compares the current minute, hour, day of month, month, and day of week against each job’s fields. If all five match, the command is executed. The command runs in the user’s shell (typically /bin/sh) with a minimal environment: HOME, LOGNAME, and SHELL are set, but PATH is often limited to /usr/bin:/bin. This is why it’s common to use absolute paths to commands or set PATH at the top of the crontab. Also, cron sends any output (stdout or stderr) to the user’s mail by default, unless you redirect it to a file or /dev/null.

Editing and Managing Your Crontab

You use the crontab command to manage your jobs:

  • crontab -e – opens your crontab in the default editor (usually vi or nano).
  • crontab -l – lists your current crontab entries.
  • crontab -r – removes your entire crontab.
  • crontab -u username – (as root) edit another user’s crontab.

When you save the file, cron notices the change and reloads it automatically. The file must end with a newline; otherwise, cron may ignore the last line. Also, the file must have correct permissions: it should not be group- or world-writable, or cron will refuse to run it.

Common Pitfalls and Misconceptions

Several issues trip up new users:

  • Environment differences: Your interactive shell has many environment variables that cron does not. Always use full paths to commands and scripts, and set any needed variables inside the crontab.
  • Day-of-month and day-of-week are OR’ed: If both fields are restricted (not *), the job runs when either matches. For example, 0 0 1 * 1 runs on the 1st of the month AND on every Monday, not only when both occur. To run only on the 1st that is also a Monday, you’d need a different approach, like checking the date in the script.
  • Percent signs: A % in the command is treated as a newline by cron. If you need a literal %, escape it with a backslash (\%).
  • Time zone: Cron uses the system’s time zone, not necessarily your local one. If you change the time zone, cron jobs follow the system clock.
  • Daylight saving time: Jobs scheduled during the “spring forward” or “fall back” hours can be skipped or run twice, depending on the system’s handling. Most cron implementations run jobs based on the local time, so a job at 2:30 AM may not run on the day DST starts.

Comparing Crontab with Other Scheduling Tools

Crontab is not the only scheduling option. Alternatives include:

  • Systemd timers: On modern Linux distributions, systemd timers can replace cron. They offer more precise control, logging, and dependency management, but are more complex to set up.
  • Anacron: For jobs that should run daily or weekly even if the system is off at the scheduled time, anacron runs missed jobs after boot. It’s often used alongside cron for /etc/cron.daily and similar directories.
  • At: The at command schedules a one-time job for a specific time, rather than recurring.
  • GUI schedulers: Tools like cronie on Linux or launchd on macOS (which uses a different format) provide similar functionality.

For most simple recurring tasks, crontab remains the standard because it’s simple, reliable, and portable across Unix-like systems.

Safety and Best Practices

When writing cron jobs, follow these guidelines to avoid problems:

  • Always test your command manually before adding it to crontab.
  • Redirect output to a log file or /dev/null to avoid filling the mail spool: command >> /var/log/myscript.log 2>&1.
  • Use absolute paths for all commands and files.
  • Set a PATH at the top of the crontab if needed, e.g., PATH=/usr/local/bin:/usr/bin:/bin.
  • Ensure scripts are executable and have the correct shebang line (e.g., #!/bin/bash).
  • Be careful with jobs that run as root; test with a non-privileged user first if possible.
  • Consider locking to prevent overlapping runs: use flock or a lock file in your script.

Frequently Asked Questions

How do I run a cron job every 5 minutes?

Use */5 * * * * in the minute field. This runs at minutes 0, 5, 10, 15, etc. If you want every 5 minutes starting at a specific minute, use a list like 2,7,12,17,... but that’s cumbersome. The step syntax is the standard way.

Why is my cron job not running?

Check the crontab syntax, ensure the cron service is running (systemctl status cron or service cron status), verify the script’s permissions and paths, and look at the system logs (often /var/log/syslog or /var/log/cron) for errors. Also, make sure the file has a trailing newline and is not world-writable.

Can I schedule a job for a specific day of the week and month together?

Yes, but remember that day-of-month and day-of-week are OR’ed. If you want a job to run only on, say, the 15th of the month and also only on Mondays (both conditions), you cannot do it directly in the crontab. Instead, schedule it on either field and add a check in your script, like if [ $(date +%d) -eq 15 ].

How do I set environment variables in crontab?

You can set them at the top of the crontab file before the job lines. For example, MAILTO=user@example.com sets where output is mailed, and PATH=/usr/local/bin:/usr/bin:/bin sets the search path. These apply to all jobs below them.

What does the MAILTO variable do?

By default, cron sends any output from a job to the user’s mail. Setting MAILTO= (empty) disables mail, or set it to an email address to send output there. This is useful for monitoring job results.

Is crontab available on Windows?

No, crontab is not native to Windows. Windows has its own Task Scheduler, or you can use WSL (Windows Subsystem for Linux) to run cron. Some third-party tools mimic cron, but the standard is Task Scheduler.

Related

How pages are sourced and corrected: Sources and editorial policy.