What Is a Cron Job and How Do You Set One Up? Linux Scheduling Guide
Use cron on Linux for automated backups, log cleanup, and script scheduling. Crontab syntax, examples, and common pitfalls.
Want automatic nightly backups, weekly log cleanup, or a 5-minute health check on your server? Cron is Linux’s standard tool. This guide covers crontab syntax, practical examples, and common pitfalls.
What is cron?
Cron is a Linux daemon that runs commands at scheduled times. It reads crontab files every minute and triggers due commands.
Crontab syntax
A cron line is 5 time fields plus a command:
* * * * * command
│ │ │ │ │
│ │ │ │ └── Day of week (0-7, 0 and 7 = Sunday)
│ │ │ └──── Month (1-12)
│ │ └────── Day of month (1-31)
│ └──────── Hour (0-23)
└────────── Minute (0-59)
Practical examples
| Expression | Meaning |
|---|---|
* * * * * | Every minute |
*/5 * * * * | Every 5 minutes |
0 * * * * | Top of every hour |
0 3 * * * | Daily at 03:00 |
0 3 * * 1 | Mondays at 03:00 |
0 3 1 * * | 1st of every month at 03:00 |
30 2 * * 1-5 | Weekdays at 02:30 |
@reboot | At server boot |
@daily | Daily at 00:00 |
@weekly | Sundays at 00:00 |
To test complex expressions: crontab.guru
How to edit crontab?
crontab -e # Edit current user's
sudo crontab -e # Edit root's
crontab -l # List
crontab -r # Delete ALL (careful!)
First time, you’ll be asked to pick an editor — nano (easy) or vim.
Practical examples
Nightly backup
# Every night at 03:00, WordPress backup
0 3 * * * /usr/local/bin/backup-wp.sh >> /var/log/backup.log 2>&1
>> /var/log/backup.log 2>&1 saves output to a log including errors.
Hourly log cleanup
0 * * * * find /var/log/myapp -name "*.log" -mtime +30 -delete
Delete logs older than 30 days.
Health check every 5 minutes
*/5 * * * * curl -s -o /dev/null -w "%{http_code}" https://yoursite.com >> /var/log/health.log
Database backup
30 2 * * * mysqldump -u root -p'PASSWORD' dbname | gzip > /backup/db-$(date +\%Y\%m\%d).sql.gz
⚠️ % is special in crontab — escape with \%.
Certificate renewal
0 4 * * * certbot renew --quiet && systemctl reload nginx
For SSL details, see our Let’s Encrypt post.
How to verify cron ran?
# Is cron running?
sudo systemctl status cron
# Cron log
sudo tail -f /var/log/syslog | grep CRON
# or
sudo tail -f /var/log/cron
To test, schedule for 1-2 minutes ahead and watch the log.
Common pitfalls
- Cron didn’t run: write commands with absolute paths (
/usr/bin/php, not justphp) - No env vars: cron runs with minimal env → set
PATH=at the script’s top - Forgetting
%:date +%Y%m%dbreaks in cron → use\% - No output visible: without mail config, STDOUT is lost → use
>> log 2>&1 - PHP/Node script timeouts: runs as CLI, no web timeout — but background long scripts
- Time zone: if server is UTC, times shift →
sudo timedatectl set-timezone Europe/Istanbul
systemd timer alternative (modern)
systemd timers are more modern than cron with cleaner log integration:
/etc/systemd/system/backup.service:
[Unit]
Description=Daily backup
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup-wp.sh
/etc/systemd/system/backup.timer:
[Unit]
Description=Daily backup timer
[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true
[Install]
WantedBy=timers.target
sudo systemctl enable --now backup.timer
sudo systemctl list-timers
Conclusion
Cron is the foundation of server automation. Learned in 5 minutes, used every day. Backups, cleanup, monitoring — all cron.
Related: Server Backups · Linux Disk Extension
Ähnliche Beiträge
Das könnte Sie auch interessieren.
Die 3-2-1-Backup-Regel: So verlieren Sie Server-Daten nie wieder
Die 3-2-1-Backup-Regel ist der Goldstandard für Server-Backup-Strategien. Wir erläutern Regel, Automation und KavesNETs Backup-Infrastruktur.
Weiterlesen
Site von Plesk zu Plesk migrieren: Migrator-Anleitung
Sites, Mail, DB und DNS in einem Schritt mit Plesk Migrator umziehen. Setup, Testmigration und Cutover.
Weiterlesen
FileZilla: Dateimigration zwischen zwei VDS
Site vom alten zum neuen VDS migrieren: FileZilla über FTP/SFTP, Geschwindigkeitstipps, Berechtigungen und Fehlerbehandlung.
Weiterlesen