Comprehensive Guide to Cron Jobs in Linux
Introduction
If you've ever wished your Linux system could do things automatically—like backing up files, sending reports, or running scripts at specific times—cron jobs are the answer. Cron is a built-in scheduler that allows you to automate tasks effortlessly, saving you time and effort.
Installing and Enabling Cron
Most Linux distributions come with cron pre-installed. To check if it's running:
crontab -l # List existing cron jobs
systemctl status cron # Check cron service status
If it's not installed, you can set it up using:
# Debian/Ubuntu
sudo apt update && sudo apt install cron
# CentOS/RHEL
sudo yum install cronie
# Enable and start the cron service
sudo systemctl enable cron
sudo systemctl start cron
Understanding Crontab Syntax
A cron job follows this format:
* * * * * command-to-be-executed
| | | | |
| | | | +---- Day of the week (0 - 7) [0 or 7 = Sunday]
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)
Special Time Strings
Instead of numbers, you can use these shortcuts:
@reboot
– Runs once at system startup.@hourly
– Runs every hour.@daily
– Runs once a day.@weekly
– Runs once a week.@monthly
– Runs once a month.@yearly
– Runs once a year.
Example:
@daily /home/user/backup.sh # Runs the backup script every day
Creating and Editing Cron Jobs
To add or modify cron jobs:
crontab -e
To see your existing jobs:
crontab -l
To remove all cron jobs:
crontab -r
Practical Cron Job Examples
Run a Job Every 5 Minutes
*/5 * * * * /home/user/script.sh
Run a Job at a Specific Time
30 2 * * * /home/user/backup.sh # Runs at 2:30 AM daily
Run a Job on Weekdays Only
0 9 * * 1-5 /home/user/work_reminder.sh # Runs at 9 AM, Monday to Friday
Run a Job on the Last Day of the Month
59 23 28-31 * * [ "$(date +\%d -d tomorrow)" == "01" ] && /home/user/monthly_report.sh
Run a Job Every 15 Minutes at Two Specific Hours
0,15,30,45 6,18 * * * /home/user/script.sh # Runs every 15 minutes at 6 AM and 6 PM
Run a Job Every Alternate Day
0 0 */2 * * /home/user/alternate_day_task.sh # Runs every other day at midnight
Run a Job Every Quarter Hour During Work Hours
*/15 9-17 * * * /home/user/script.sh # Runs every 15 minutes between 9 AM and 5 PM
Logging and Debugging Cron Jobs
If your cron job isn't running, you can check its logs:
tail -f /var/log/syslog # Debian/Ubuntu
journalctl -u cron -f # Systemd-based systems
To capture output:
* * * * * /home/user/script.sh >> /var/log/script.log 2>&1
System-wide Cron Jobs
Admins can define cron jobs for all users in /etc/crontab
or /etc/cron.d/
.
sudo nano /etc/crontab
Format:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
0 3 * * * root /usr/bin/updatedb
Final Thoughts
Mastering cron jobs means you'll never have to worry about forgetting to run a script or task again. Whether it's simple automation or advanced scheduling, cron is a powerful tool that can make your Linux life much easier!