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.
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 |
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 |
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 |
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 |
crontab -e
- Edit your user’s crontab file.crontab -l
- List the contents of your crontab file.crontab -r
- Remove your crontab file.sudo crontab -e
- Edit the root user's crontab file.crontab -u username -e
- Edit the crontab file for a specific user (requires appropriate permissions).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
/usr/bin/python3
)./dev/null
if not needed.
0 * * * * command > /dev/null 2>&1
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) |
# 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 /tmp directory every week on Sunday at 3 AM
0 3 * * SUN rm -rf /tmp/* > /dev/null 2>&1
# Renew SSL certificates on the first day of every month at 4 AM
0 4 1 * * /usr/bin/certbot renew --quiet
# 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
Cron recognizes several special environment variables:
MAILTO
- Specifies the email address to send output to. Set to an empty string to disable email.
MAILTO="[email protected]"
PATH
- Specifies the search path for commands.
PATH=/usr/local/bin:/usr/bin:/bin
SHELL
- Specifies the shell to use (default is /bin/sh
).
SHELL=/bin/bash
HOME
- Specifies the home directory to use for the cron job.
HOME=/home/user
System-wide cron jobs can be found in the following directories and files:
/etc/crontab
- System-wide crontab file./etc/cron.d/
- Directory for system cron jobs./etc/cron.hourly/
- Scripts to run hourly./etc/cron.daily/
- Scripts to run daily./etc/cron.weekly/
- Scripts to run weekly./etc/cron.monthly/
- Scripts to run monthly.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>
Access to cron is controlled by the files /etc/cron.allow
and /etc/cron.deny
:
/etc/cron.allow
exists, only users listed in it can use cron
./etc/cron.allow
does not exist but /etc/cron.deny
exists, users listed in cron.deny
cannot use cron
.cron
./var/log/cron
, /var/log/syslog
, or /var/log/messages
.#!/bin/bash
).