Automate with Cron
This guide explains how to automate tasks on your Linux VPS with cron.
What is Cron?β
Cron is a task scheduler built into Linux. It allows you to automatically run commands or scripts at defined intervals.
Cron Syntaxβ
ββββββββββββββ minute (0 - 59)
β ββββββββββββββ hour (0 - 23)
β β ββββββββββββββ day of month (1 - 31)
β β β ββββββββββββββ month (1 - 12)
β β β β ββββββββββββββ day of week (0 - 6) (Sunday = 0)
β β β β β
* * * * * command to execute
Edit the crontabβ
# Edit current user's tasks
crontab -e
# Edit root's tasks
sudo crontab -e
# View scheduled tasks
crontab -l
Common Examplesβ
Periodic Executionβ
# Every minute
* * * * * /path/to/script.sh
# Every 5 minutes
*/5 * * * * /path/to/script.sh
# Every hour
0 * * * * /path/to/script.sh
# Every day at 3 AM
0 3 * * * /path/to/script.sh
# Every Monday at 8 AM
0 8 * * 1 /path/to/script.sh
# 1st of every month at midnight
0 0 1 * * /path/to/script.sh
Useful Shortcutsβ
@reboot # At server startup
@hourly # Every hour (equivalent to 0 * * * *)
@daily # Every day at midnight
@weekly # Every Sunday at midnight
@monthly # 1st of the month at midnight
@yearly # January 1st at midnight
Practical Use Casesβ
Automatic Database Backupβ
# Create the backup script
nano /root/scripts/backup-db.sh
#!/bin/bash
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/root/backups"
# Create folder if it doesn't exist
mkdir -p $BACKUP_DIR
# MySQL/MariaDB backup
mysqldump -u root -p'PASSWORD' my_database > $BACKUP_DIR/my_database_$DATE.sql
# Compress
gzip $BACKUP_DIR/my_database_$DATE.sql
# Delete backups older than 7 days
find $BACKUP_DIR -name "*.sql.gz" -mtime +7 -delete
# Make executable
chmod +x /root/scripts/backup-db.sh
# Add to cron (every day at 4 AM)
crontab -e
0 4 * * * /root/scripts/backup-db.sh
Automatic Service Restartβ
# Restart Nginx every day at 5 AM
0 5 * * * systemctl restart nginx
# Restart a FiveM server every 6 hours
0 */6 * * * systemctl restart fivem
Automatic Log Cleanupβ
# Delete logs older than 30 days
0 3 * * * find /var/log -name "*.log" -mtime +30 -delete
Automatic System Updateβ
# Security update every day at 2 AM
0 2 * * * apt update && apt upgrade -y
Disk Space Monitoringβ
# Create the script
nano /root/scripts/check-disk.sh
#!/bin/bash
THRESHOLD=80
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')
if [ $USAGE -gt $THRESHOLD ]; then
echo "Alert: Disk at ${USAGE}%" | mail -s "VPS Disk Alert" your@email.com
fi
# Check every hour
0 * * * * /root/scripts/check-disk.sh
Logs and Debuggingβ
Redirect Outputβ
# To a log file
0 3 * * * /root/scripts/backup.sh >> /var/log/backup.log 2>&1
# Ignore output
0 3 * * * /root/scripts/backup.sh > /dev/null 2>&1
# Receive by email (if mail is configured)
0 3 * * * /root/scripts/backup.sh
View Cron Logsβ
# On Ubuntu/Debian
grep CRON /var/log/syslog
# Or
journalctl -u cron
Environment Variablesβ
Add them at the top of your crontab:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MAILTO=your@email.com
# Your tasks
0 3 * * * /root/scripts/backup.sh
Useful Toolsβ
Crontab Guruβ
Use crontab.guru to verify your cron expressions.
Anacronβ
For tasks that must run even if the server was down:
sudo apt install anacron
Tip
Always use absolute paths in your cron scripts, as the environment is different from your interactive session.