Cronjobs – Scheduling Tasks Automatically
Cronjobs – Scheduling Tasks Automatically – Server administration and Linux knowledge from NexoraHost.
With cronjobs you automate recurring tasks on your server – from backups to updates to deleting old logs. This guide shows you how to set up and manage cronjobs.
Note: Detailed guides on server administration can be found in the left navigation.
What is a cronjob?
A cronjob is a time-controlled task that the cron daemon runs in the background. You define when and how often a command or script is executed – once or regularly. Cron runs on every Linux server and is active by default.
Understanding cronjob syntax
A cronjob consists of five time fields and the command to execute:
* /path/to/command │ │ │ │ │ │ │ │ │ └── Day of week (0-7, 0 and 7 = Sunday) │ │ │ └──── Month (1-12) │ │ └────── Day of month (1-31) │ └──────── Hour (0-23) └────────── Minute (0-59)
Time fields – examples
Schedule Syntax Meaning
Every minute * Runs 1440 times a day
Every hour 0 At the top of the hour
Daily at 3 AM 0 3 * Good for backups and maintenance
Every Sunday at 4 AM 0 4 0 Weekly tasks
1st of every month 0 0 1 Monthly reports
Every 15 minutes /15 * Frequent checks
Weekdays at 9 AM 0 9 1-5 Monday to Friday
Setting up cronjobs
For the current user
# Open crontab crontab -e The first time you'll be asked for an editor. Choose nano (easier) or vim (advanced).
For root (system tasks)
sudo crontab -e
For a specific user
sudo crontab -u username -e
Practical examples
Backup automation
`# Daily database backup at 2:30 AM 30 2 * mysqldump -u root -p'Password' database > /backup/db_$(date +\%Y\%m\%d).sql
Weekly server backup every Sunday at 4 AM
0 4 0 tar -czf /backup/server_$(date +\%Y\%m\%d).tar.gz /var/www`
Log management
`# Delete logs older than 30 days 0 5 find /var/log -name ".log" -mtime +30 -delete
Clean up Docker logs
0 6 0 docker system prune -af`
Updates and maintenance
`# System updates every Monday at 3 AM 0 3 1 apt update && apt upgrade -y
Restart a server once daily (for game servers)
0 4 * systemctl restart minecraft`
Monitoring
# Disk space check with email warning 0 8 * df -h | awk '{if ($5+0 > 90) print "Warning: " $6 " is " $5 " full"}' | mail -s "Disk Space Warning" admin@domain.com
Managing cronjobs
`# List all cronjobs of the current user crontab -l
Delete all cronjobs (Caution!)
crontab -r
Show all cronjobs of all users
for user in $(cut -f1 -d: /etc/passwd); do echo "=== $user ==="; sudo crontab -u $user -l 2>/dev/null; done`
Output and logging
By default, the output of a cronjob is sent via email to the user. You can control this:
`# Write output to file 0 3 * /path/to/script.sh >> /var/log/my-cron.log 2>&1
Discard output (silent)
0 3 * /path/to/script.sh > /dev/null 2>&1
Only log errors
0 3 * /path/to/script.sh >> /var/log/my-cron.log 2>> /var/log/my-cron-error.log`
System-wide cronjobs (/etc/crontab)
In addition to user-specific crontabs, there are system-wide cronjobs. These have an additional field for the user:
`# In /etc/crontab
m h dom mon dow user command
0 3 * root /usr/local/bin/backup.sh`
Pre-configured directories for scripts:
- /etc/cron.hourly/ – Runs every hour
- /etc/cron.daily/ – Runs daily
- /etc/cron.weekly/ – Runs weekly
- /etc/cron.monthly/ – Runs monthly
Simply place an executable script in the corresponding directory.
Common problems
Cronjob not running:
- Check if the cron daemon is running:
sudo systemctl status cron - Use absolute paths – cron doesn't know your PATH environment
- Check the cron logs:
grep CRON /var/log/syslog
Percent signs (%) in commands:
- Percent signs have special meaning in crontabs. Escape them with a backslash:
\%
Script is executable but doesn't run via cron:
- Cron has a minimal environment. Define SHELL and PATH at the top of your crontab:
`SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
0 3 * /path/to/script.sh`
Detailed guides on scripts and automation can be found in the articles in the left navigation.