What is Crontab?
Crontab is a powerful time-based job scheduler in Unix/Linux systems. It allows users to schedule commands or scripts to run automatically at specified times, dates, or intervals without manual intervention.
Installing and Starting Crontab
On CentOS / RHEL / Fedora Systems
The cronie package provides the crontab service. Install and configure it using:
# Install crontab service
sudo yum install cronie
# Enable auto-start on boot
sudo systemctl enable crond
# Start the service
sudo systemctl start crond
# Check service status
sudo systemctl status crond
Note: On newer CentOS/RHEL 7+ and Fedora systems, service management uses systemctl instead of service and chkconfig.
On Debian / Ubuntu Systems
The cron service is usually pre-installed. To install or manage it:
# Install cron (usually already installed)
sudo apt-get install cron
# Start/restart service
sudo systemctl restart cron
# Enable auto-start on boot
sudo systemctl enable cron
Basic Crontab Usage
1. List Current User's Scheduled Tasks
crontab -l
2. Edit Current User's Scheduled Tasks
crontab -e
This opens the user's crontab file in the default text editor (usually vim or nano).
- Using nano: Edit, then press
Ctrl+X, typeYto confirm, and pressEnterto exit. - Using vim: Press
ito insert, edit, pressESC, then type:wqto save and quit.
3. Delete All Tasks for Current User
crontab -r
Warning: This deletes all tasks immediately. Use with caution.
Crontab Time Format Explained
A crontab entry consists of a schedule and a command:
* * * * * command-to-execute
│ │ │ │ │
│ │ │ │ └── Day of week (0-7, 0 & 7 = Sunday)
│ │ │ └──── Month (1-12)
│ │ └────── Day of month (1-31)
│ └──────── Hour (0-23)
└────────── Minute (0-59)
Special characters for time fields:
| Symbol | Meaning | Example |
|---|---|---|
* |
All valid values | 0 * * * * = every hour at minute 0 |
, |
List of values | 0 8,12,18 * * * = at 8, 12, 18 o'clock |
- |
Range of values | 0 9-17 * * * = every hour from 9 to 17 |
/ |
Step values | */15 * * * * = every 15 minutes |
Common Crontab Examples
# Daily backup at 3 AM
0 3 * * * /root/scripts/backup.sh
# Weekly log cleanup Sunday at 8:30 PM
30 20 * * 7 /root/scripts/clean_logs.sh
# Test every Monday and Friday at midnight
0 0 * * 1,5 /usr/local/bin/test
# Annual task on May 12 at 2 PM
0 14 12 5 * /root/scripts/memorial.sh
# Restart service every 15 min from 6 PM to 11 PM
*/15 18-23 * * * systemctl restart php-fpm
Advanced Tips and Considerations
1. Environment Variables
Cron jobs run in a minimal environment. Use absolute paths or define variables at the top of your crontab:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# Your tasks below
0 * * * * /full/path/to/script.sh
2. Output Redirection
By default, cron sends output via email. Redirect it to avoid clogging your mailbox:
# Redirect both stdout and stderr to a log file
0 3 * * * /root/backup.sh > /var/log/backup.log 2>&1
# Discard all output
0 3 * * * /root/backup.sh > /dev/null 2>&1
3. System-Level Crontab
System administrators can edit /etc/crontab or files in /etc/cron.d/ for system-wide jobs. These require specifying the user:
# Format in /etc/crontab
# m h dom mon dow user command
0 5 * * * root /root/scripts/system_backup.sh
With this knowledge, you can effectively use Crontab to automate system administration, backups, log rotation, and other routine tasks.