How to Use Cron Jobs for Scheduled Automation
Cron is the standard job scheduler on Linux systems. It lets you run scripts and commands at specified intervals on your Breeze instance, automating backups, log rotation, health checks, and more.
Understanding Crontab Syntax
A cron expression has five time fields followed by the command to run:
# ┌───────────── minute (0-59)
# │ ┌───────────── hour (0-23)
# │ │ ┌───────────── day of month (1-31)
# │ │ │ ┌───────────── month (1-12)
# │ │ │ │ ┌───────────── day of week (0-7, 0 and 7 = Sunday)
# │ │ │ │ │
# * * * * * command_to_execute
Managing Crontabs
Each user has their own crontab. Edit it with:
# Edit current user's crontab
crontab -e
# View current crontab
crontab -l
# Edit another user's crontab (as root)
sudo crontab -u www-data -e
# Remove all cron jobs (use with caution!)
crontab -r
Common Cron Schedules
# Every 5 minutes
*/5 * * * * /usr/local/bin/health-check.sh
# Every hour at minute 0
0 * * * * /usr/local/bin/sync-data.sh
# Daily at 3:00 AM
0 3 * * * /usr/local/bin/backup.sh
# Every Monday at 6:00 AM
0 6 * * 1 /usr/local/bin/weekly-report.sh
# First day of every month at midnight
0 0 1 * * /usr/local/bin/monthly-cleanup.sh
# Every weekday at 9:00 AM
0 9 * * 1-5 /usr/local/bin/business-report.sh
# Twice daily at 6 AM and 6 PM
0 6,18 * * * /usr/local/bin/certificate-check.sh
Handling Output and Errors
Cron sends output via email by default. Redirect output to log files instead:
# Log stdout and stderr to a file
0 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# Discard all output (not recommended for important tasks)
*/5 * * * * /usr/local/bin/cleanup.sh > /dev/null 2>&1
# Log stdout and stderr separately
0 3 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>> /var/log/backup-error.log
# Set a MAILTO address for cron notifications
MAILTO=admin@example.com
0 3 * * * /usr/local/bin/backup.sh
Preventing Overlapping Runs
Use flock to ensure only one instance of a cron job runs at a time:
# Use flock to prevent overlapping runs
*/5 * * * * /usr/bin/flock -n /tmp/health-check.lock /usr/local/bin/health-check.sh
# With a timeout (wait up to 60 seconds for lock)
*/5 * * * * /usr/bin/flock -w 60 /tmp/sync.lock /usr/local/bin/sync-data.sh
Environment Variables in Cron
Cron runs with a minimal environment. Set variables explicitly:
# Set environment at the top of crontab
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOME=/root
# Or source a profile in your script
0 3 * * * /bin/bash -lc '/usr/local/bin/backup.sh'
System-Wide Cron
Use /etc/cron.d/ for system-level jobs that include a user field:
# /etc/cron.d/app-backup
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
0 3 * * * root /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
Debugging Cron Jobs
- Check cron logs:
grep CRON /var/log/syslog - Verify script permissions: ensure the script is executable (
chmod +x) - Test manually first: run the exact command from the cron entry in your shell
- Check PATH: use absolute paths for all commands in scripts
- Verify cron is running:
systemctl status cron
Cron jobs are essential for automating recurring tasks on your Breeze instances, from simple cleanups to complex data pipelines.