Crontab Linux: Complete Guide with Practical Examples
Linux crontab guide: syntax, 10 practical examples, output management and troubleshooting.
Every night at 3 AM you need to backup the database. Every Monday morning you need to clean old logs. Every hour you need to check if the service is running. You could set alarms and remember to run these commands manually... or you can let cron do it for you, forever, without ever forgetting.
In this guide, we'll cover how to use crontab on Linux to automate any task, from basic syntax to ready-to-copy practical examples.
What is Cron and Crontab
Cron is a daemon (background service) present in all Linux and Unix systems that executes commands at scheduled intervals. Crontab (cron table) is the file where you define what to run and when.
Each user has their own crontab. Root has theirs (for system tasks), and you have yours (for your scripts). Simple, powerful, reliable for decades.
Essential commands
# View your crontab
crontab -l
# Edit your crontab
crontab -e
# Remove your crontab (careful!)
crontab -r
# View another user's crontab (requires root)
sudo crontab -u username -l
Crontab syntax
Each line in the crontab follows this format:
* * * * * command
ā ā ā ā ā
ā ā ā ā āāā Day of week (0-7, 0 and 7 = Sunday)
ā ā ā āāāāā Month (1-12)
ā ā āāāāāāā Day of month (1-31)
ā āāāāāāāāā Hour (0-23)
āāāāāāāāāāā Minute (0-59)
Special characters:
*= every possible value,= list of values (e.g., 1,15 = 1st and 15th)-= range (e.g., 1-5 = Monday to Friday)/= every N (e.g., */15 = every 15 minutes)
10 practical examples (copy and paste)
1. Every minute
* * * * * /path/to/script.sh
Useful for monitoring or health checks of critical services.
2. Every 5 minutes
*/5 * * * * /path/to/script.sh
3. Every hour (at minute 0)
0 * * * * /path/to/script.sh
4. Every day at 3 AM
0 3 * * * /path/to/backup.sh
The classic time for backups and maintenance.
5. Every Monday at 9:00 AM
0 9 * * 1 /path/to/weekly-report.sh
1 = Monday (0 and 7 = Sunday)
6. First day of every month
0 0 1 * * /path/to/monthly-task.sh
7. Weekdays at 6:00 PM
0 18 * * 1-5 /path/to/end-of-day.sh
1-5 = Monday to Friday
8. Twice a day (9:00 AM and 6:00 PM)
0 9,18 * * * /path/to/script.sh
9. Every 30 minutes during business hours
*/30 9-18 * * 1-5 /path/to/check.sh
From 9 AM to 6 PM, Monday-Friday, every 30 minutes.
10. Special strings (shortcuts)
@reboot /path/to/startup.sh # At system startup
@hourly /path/to/script.sh # Every hour (= 0 * * * *)
@daily /path/to/script.sh # Every day (= 0 0 * * *)
@weekly /path/to/script.sh # Every week (= 0 0 * * 0)
@monthly /path/to/script.sh # Every month (= 0 0 1 * *)
@yearly /path/to/script.sh # Every year (= 0 0 1 1 *)
Real-world use cases
Automatic MySQL database backup
# Backup every night at 2:30 AM
30 2 * * * mysqldump -u root -p'password' database > /backup/db_$(date +\%Y\%m\%d).sql
# With compression and 7-day retention
30 2 * * * mysqldump -u root database | gzip > /backup/db_$(date +\%Y\%m\%d).sql.gz && find /backup -name "*.gz" -mtime +7 -delete
Clean old logs
# Every Sunday, delete logs older than 30 days
0 4 * * 0 find /var/log/myapp -name "*.log" -mtime +30 -delete
Service monitoring
# Every 5 minutes, check if nginx is running
*/5 * * * * systemctl is-active nginx || systemctl restart nginx
File synchronization
# Every hour, sync with remote server
0 * * * * rsync -avz /local/folder/ user@remote:/backup/folder/
Managing output and logs
By default, cron sends output via email (if configured). To handle it differently:
# Discard all output
0 3 * * * /path/to/script.sh > /dev/null 2>&1
# Log to a file
0 3 * * * /path/to/script.sh >> /var/log/mycron.log 2>&1
# Only errors to a file
0 3 * * * /path/to/script.sh > /dev/null 2>> /var/log/mycron-errors.log
Explanation: 2>&1 redirects stderr to stdout. > /dev/null discards stdout.
Common mistakes to avoid
- Relative paths: cron doesn't know where you are. Always use absolute paths (
/usr/bin/python, notpython) - Environment variables: cron has a minimal environment. Define PATH or necessary variables in crontab
- Permissions: make sure the script is executable (
chmod +x script.sh) - % in command: in crontab,
%has special meaning (newline). Escape it with\% - Not testing first: always run the script manually before scheduling it
Debug: cron not working?
# Check if cron is running
systemctl status cron
# Check cron logs
grep CRON /var/log/syslog
# Add temporary logging for debug
* * * * * echo "Cron works at $(date)" >> /tmp/cron-test.log
Conclusion
Cron is one of Linux's most underrated tools. Once configured, it works silently 24/7 without ever forgetting. With this guide you can:
- Automate daily backups
- Schedule cleanup and maintenance
- Monitor services and send alerts
- Sync files between servers
The most important tip: start simple, always test manually, and add logging to understand if everything works as expected.
Try our free crontab generator to create cron expressions visually without remembering the syntax.