Cron Job Reference

The cron utility is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals. This reference provides an overview of how to write cron expressions, manage cron jobs, and includes examples for common scheduling tasks.

Understanding the Cron Syntax

A typical cron job consists of a line in the crontab file with the following format:

<minute> <hour> <day_of_month> <month> <day_of_week> <command>

The fields are separated by spaces or tabs and have the following meanings:

Field Allowed Values Description
minute 0-59 Minute of the hour
hour 0-23 Hour of the day
day_of_month 1-31 Day of the month
month 1-12 or JAN-DEC Month of the year
day_of_week 0-6 or SUN-SAT Day of the week (0 or 7 is Sunday)
command Any valid command or script The task to execute

Special Characters in Cron Expressions

Character Description Example
* Matches all possible values * * * * * (Every minute)
, Separates multiple values 0,15,30,45 * * * * (Every 15 minutes)
- Defines a range of values 1-5 (From 1 to 5)
/ Specifies step values */5 (Every 5 units)
# Specifies the nth occurrence of a weekday in a month (non-standard, Vixie cron) 1#2 (Second Monday of the month)
L Specifies the last day of the week or month (non-standard) LW (Last weekday of the month)
? No specific value (used in day_of_month and day_of_week fields, non-standard)
% Newline in the command

Special Scheduling Strings

Cron also supports special strings in place of the five time fields:

String Equivalent Description
@reboot N/A Run once at startup
@yearly or @annually 0 0 1 1 * Once a year at midnight on January 1st
@monthly 0 0 1 * * Once a month at midnight on the first day
@weekly 0 0 * * 0 Once a week at midnight on Sunday
@daily or @midnight 0 0 * * * Once a day at midnight
@hourly 0 * * * * Once an hour at the beginning of the hour

Common Examples

Cron Expression Description
0 * * * * command Every hour at minute 0 (e.g., 1:00, 2:00)
*/15 * * * * command Every 15 minutes
0 6 * * * command Every day at 6:00 AM
30 21 * * 1-5 command At 9:30 PM every weekday (Monday through Friday)
0 0 1 * * command At midnight on the first day of every month
0 0 * * 0 command Every Sunday at midnight
0 8-18/2 * * * command Every 2 hours between 8 AM and 6 PM
0 0 1 1 * command Once a year on January 1st at midnight
@reboot command Run once at system startup

Managing Cron Jobs

Crontab File Format

The crontab file can include environment variable settings and comments:

# This is a comment
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin

# Run backup script every day at 2 AM
0 2 * * * /home/user/backup.sh

# Run cleanup script on Sundays at 3 AM
0 3 * * SUN /home/user/cleanup.sh

Tips for Writing Cron Jobs

Advanced Scheduling Examples

Cron Expression Description
0 0 * * MON command Every Monday at midnight
0 9-17 * * MON-FRI command Every hour from 9 AM to 5 PM on weekdays
*/10 6-18 * * * command Every 10 minutes between 6 AM and 6 PM every day
0 0 1 */2 * command At midnight on the first day of every even-numbered month
0 0 * * 1 command Every Monday at midnight
0 0 1 1-6/2 * command At midnight on the first day of every other month from January through June
0 12 * * * [ "$(date '+\%d')" -le 07 ] && command At noon on the first seven days of the month
0 0 29 2 * command At midnight on February 29th (leap year)

Cron Job Examples

Backup Home Directory

# Backup home directory to /backup at 2 AM every day
0 2 * * * tar -zcf /backup/home_$(date +\%Y\%m\%d).tar.gz /home/user/ > /dev/null 2>&1

Clear Temporary Files

# Clear /tmp directory every week on Sunday at 3 AM
0 3 * * SUN rm -rf /tmp/* > /dev/null 2>&1

Renew Let's Encrypt SSL Certificates

# Renew SSL certificates on the first day of every month at 4 AM
0 4 1 * * /usr/bin/certbot renew --quiet

Run a Python Script

# Execute Python script every day at 6 PM
0 18 * * * /usr/bin/python3 /home/user/scripts/daily_report.py >> /var/log/daily_report.log 2>&1

Special Environment Variables

Cron recognizes several special environment variables:

Editing System-Wide Cron Jobs

System-wide cron jobs can be found in the following directories and files:

The system-wide crontab file and files in /etc/cron.d/ have an additional field specifying the user:

<minute> <hour> <day_of_month> <month> <day_of_week> <user> <command>

Cron Allow and Deny Files

Access to cron is controlled by the files /etc/cron.allow and /etc/cron.deny:

Troubleshooting Cron Jobs

Return to Home