Cron executes scheduled commands at specified times. Edit your crontab with crontab -e. The format is: minute hour day-of-month month day-of-week command. Examples: 0 2 * * * /scripts/backup.sh (daily at 2 AM), */5 * * * * /scripts/check-health.sh (every 5 minutes). Always redirect output to a log file: command >> /var/log/mycron.log 2>&1. Use absolute paths for everything — cron has a minimal PATH. Test your scripts manually before scheduling them.
Cron Basics and Best Practices
Cron executes scheduled commands at specified times. Edit your crontab with crontab -e. The format is: minute hour day-of-month month day-of-week command. Examples: 0 2 * * * /scripts/backup.sh (daily at 2 AM), */5 * * * * /scripts/check-health.sh (every 5 minutes). Always redirect output to a log file: command >> /var/log/mycron.log 2>&1. Use absolute paths for everything — cron has a minimal PATH. Test your scripts manually before scheduling them.
Essential Cron Recipes for VPS
Database backups: 0 3 * * * mysqldump --all-databases | gzip > /backups/db-$(date +%Y%m%d).sql.gz. SSL renewal check: 0 0 1 * * certbot renew --quiet. Log cleanup: 0 4 * * 0 find /var/log -name '*.gz' -mtime +30 -delete. Security updates: 0 6 * * 1 apt update && apt upgrade -y -o Dpkg::Options::='--force-confold'. Cache purge: 0 */6 * * * redis-cli FLUSHDB.
Error Handling and Monitoring
Don’t let cron jobs fail silently. Add error checking to scripts: set -e to exit on errors, trap for cleanup on failure. Set up email notifications for failures: configure [email protected] at the top of your crontab. Use lock files to prevent overlapping runs: flock -n /tmp/script.lock /scripts/long-task.sh. Monitor cron logs at /var/log/syslog | grep CRON. Well-automated FastNode VPS instances practically manage themselves.
Looking for reliable hosting? FastNode offers Lightning-fast UK VPS hosting with NVMe storage and low-latency servers across Europe. Explore our plans and find the perfect solution for your needs.