tech8 min read

Complete Guide to Crontab Scheduling

Master cron job scheduling on Linux and macOS. Learn crontab syntax, common patterns, debugging tips, and best practices for automated task scheduling.

ShareY
1

Understand Crontab Syntax

A crontab expression has five fields: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-7, where 0 and 7 are Sunday). An asterisk means every value. For example, '30 2 * * 1' runs at 2:30 AM every Monday. Use our crontab guru tool to visually build and validate expressions. Edit your crontab with 'crontab -e' and list existing jobs with 'crontab -l'.

2

Learn Common Patterns

The most frequent cron patterns are: '0 * * * *' (every hour on the hour), '*/5 * * * *' (every 5 minutes), '0 9 * * 1-5' (weekdays at 9 AM), '0 0 1 * *' (first day of each month at midnight), '0 0 * * 0' (every Sunday at midnight), and '@reboot' (on system startup). Ranges use hyphens (1-5 for Monday through Friday), lists use commas (1,15 for the 1st and 15th), and steps use slashes (*/10 for every 10).

3

Handle Output and Errors

By default, cron emails command output to the system user. Redirect output to a log file: '0 * * * * /script.sh >> /var/log/script.log 2>&1'. The 2>&1 captures both stdout and stderr. To discard output entirely: '> /dev/null 2>&1'. Always log output for debugging — silent failures are the most common cron problem. Set MAILTO=your@email.com at the top of crontab for error notifications.

4

Debug and Best Practices

Common issues: scripts work manually but fail in cron because cron uses a minimal PATH. Fix by using full paths to commands (/usr/bin/python3 not python3) or setting PATH at the top of your crontab. Always test scripts manually first. Use 'flock' to prevent overlapping runs of long-running scripts. Set appropriate file permissions — cron scripts need execute permission (chmod 755).

Pro Tips

  • Use crontab -l to list current jobs before editing — prevents accidental overwrites
  • Add comments above each cron entry explaining what it does and when it was added
  • Test with a simple cron first (every minute) before setting the final schedule
  • Use anacron for tasks on laptops that may not be running at the scheduled time

Frequently Asked Questions

What timezone does cron use?

Cron uses the system timezone by default (check with 'timedatectl' or 'date'). You can override this by setting TZ=America/New_York at the top of your crontab. Be careful with UTC versus local time, especially around daylight saving transitions. Some cron implementations handle DST differently — jobs scheduled during the skipped hour may not run.

Why is my cron job not running?

The most common reasons are: wrong PATH (use full paths to commands), permission issues (script not executable), syntax errors in the cron expression, script errors that fail silently (check logs), and the cron service not running (check with 'systemctl status cron'). Add output logging to every cron job and check /var/log/syslog for cron execution records.

How do I run a cron job every 5 minutes?

Use the expression '*/5 * * * * /path/to/script.sh'. The */5 in the minutes field means every 5th minute. You can apply this pattern to any field: */2 in the hours field means every 2 hours, */3 in the day-of-week field means every 3rd day. For less common intervals like every 45 minutes, list specific minutes: '0,45 * * * *'.