A Beginner’s Guide to Cron: Scheduling Tasks Like a Pro
Cron is a time-based job scheduler in Unix-like operating systems. It runs tasks (called "cron jobs") automatically at specified intervals—whether daily, weekly, or even every minute.
Common uses include:
Cron jobs are defined in a crontab (cron table) file. Each line represents a job and follows this structure:
* * * * * command-to-execute
│ │ │ │ │
│ │ │ │ └── Day of the week (0 - 7, where 0 and 7 = Sunday)
│ │ │ └──── Month (1 - 12)
│ │ └────── Day of the month (1 - 31)
│ └──────── Hour (0 - 23)
└────────── Minute (0 - 59)
*
= Any value (e.g., every minute, every hour),
= List multiple values (e.g., 1,15,30
)-
= Range (e.g., 1-5
for Monday to Friday)/
= Step values (e.g., */5
for every 5 minutes)To create or modify cron jobs, use:
crontab -e # Opens your user's crontab in the default editor
To list your scheduled jobs:
crontab -l
To remove all cron jobs:
crontab -r
(Be careful—this deletes everything!)
0 3 * * * /path/to/script.sh
*/5 * * * * /usr/bin/php /var/www/cleanup.php
0 0 * * 0 /usr/bin/rsync -a /home/user/backup/ /mnt/backup/
0 17 * * 1-5 /usr/bin/python3 /home/user/script.py
/var/log/syslog
or /var/log/cron
.>>
or 2>&1
:
* * * * * /path/to/script.sh >> /var/log/cron.log 2>&1
Cron is an essential tool for automating tasks on Unix-like systems. With just a few commands, you can schedule jobs to run at precise times, saving you time and effort.
Now that you know the basics, try setting up your first cron job!
Got questions? Drop them in the comments below!
Happy scheduling!