bashbeginner

Linux Cron Job Setup Examples

Common crontab entries for database backups, log cleanup, health checks, and certificate renewal.

bash
#!/usr/bin/env bash
# Edit crontab: crontab -e
# List entries: crontab -l
# Format: MIN HOUR DOM MON DOW COMMAND

# Database backup every day at 2 AM
0 2 * * * /opt/scripts/backup-db.sh >> /var/log/backup.log 2>&1

# Clean logs older than 7 days every Sunday
0 3 * * 0 find /var/log/app -name '*.log' -mtime +7 -delete

# Health check every 5 minutes
*/5 * * * * curl -sf http://localhost:3000/health || echo "Health check failed" | mail -s "Alert" admin@example.com

# SSL certificate renewal check twice daily
0 0,12 * * * certbot renew --quiet --post-hook "systemctl reload nginx"

# Clear temp files every hour
0 * * * * find /tmp/app-cache -mmin +60 -delete 2>/dev/null

# Weekly report generation Monday 8 AM
0 8 * * 1 /opt/scripts/generate-report.sh

# Monthly disk usage alert
0 9 1 * * df -h | mail -s "Disk Report" admin@example.com

# Backup script example
# /opt/scripts/backup-db.sh
# #!/usr/bin/env bash
# set -euo pipefail
# TIMESTAMP=$(date +%Y%m%d_%H%M%S)
# pg_dump -U appuser appdb | gzip > /backups/appdb_$TIMESTAMP.sql.gz
# find /backups -name '*.sql.gz' -mtime +30 -delete

Use Cases

  • Automated database backups
  • Log rotation and cleanup
  • Scheduled health monitoring

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.