The crontab command is used to schedule tasks (cron jobs) to run automatically at specific times or intervals. It’s part of the standard Linux scheduling system powered by the cron daemon.
📌 What is Crontab?
A crontab (cron table) is a file that contains scheduled commands. Each user can have their own crontab, allowing automation like:
- Running backups
- Updating systems
- Executing scripts
▶️ Basic Crontab Commands
Edit your crontab
crontab -e
List current cron jobs
crontab -l
Remove all cron jobs
crontab -r
Edit another user’s crontab (root only)
sudo crontab -u username -e
⏱️ Crontab Syntax Explained
Each cron job follows this format:
* * * * * command
│ │ │ │ │
│ │ │ │ └── Day of week (0–7) (Sun = 0 or 7)
│ │ │ └──── Month (1–12)
│ │ └────── Day of month (1–31)
│ └──────── Hour (0–23)
└────────── Minute (0–59)
🧪 Common Examples
Run a script every day at 2:30 AM
30 2 * * * /home/user/script.sh
Run every 5 minutes
*/5 * * * * /home/user/script.sh
Run every Sunday at midnight
0 0 * * 0 /home/user/script.sh
Reboot system every day at 3 AM
0 3 * * * /sbin/reboot
📂 Log Output (Important)
By default, cron jobs don’t show output. Redirect logs like this:
0 2 * * * /home/user/script.sh >> /home/user/cron.log 2>&1
✔ Helps with debugging
✔ Captures errors
⚠️ Common Issues (and Fixes)
Script doesn’t run
- Use full paths (cron doesn’t load your environment)
- Example:
/usr/bin/python3 /home/user/script.py
Permission denied
chmod +x script.sh
Cron service not running
sudo systemctl status cron
Start it if needed:
sudo systemctl start cron
🔐 Crontab Locations (Advanced)
User crontabs:
/var/spool/cron/crontabs/System-wide cron:
/etc/crontab
🧠 Pro Tips
- Always test commands manually first
- Use logging to track failures
- Avoid running heavy jobs too frequently
- Use
crontab.guru(online tool) to validate syntax
🔄 When to Use Cron
- Automated backups
- Log rotation
- Software updates
- Monitoring scripts