Cron Jobs for DevOps: Scheduling Automation the Right Way
Table of Contents
Cron jobs power most DevOps automation: nightly builds, log rotation, database backups, certificate renewals, cache invalidation. Done right, they run silently in the background for years. Done wrong, they create silent failures, overlapping processes, and 3 AM incidents. This guide covers what experienced DevOps engineers actually do.
Build your schedules with our free cron expression generator and verify timing with the crontab visualizer before deploying.
The 5 Most Common DevOps Cron Mistakes
- No output logging. Cron discards stdout/stderr by default (or emails it, which nobody reads). Add
>> /var/log/job.log 2>&1to every job or set up centralized log forwarding. - Overlapping executions. A backup job scheduled every 5 minutes takes 7 minutes to run. The 2nd run starts while the 1st is still running. Use file-based locking:
flock -n /tmp/job.lock /path/to/script.sh - No alerting on failure. Cron doesn't notify you when a job exits non-zero. At minimum, add error-exit alerting to critical scripts. Better: use a monitoring service like Healthchecks.io or Cronitor.
- Timezone confusion. Server is UTC, developer thinks in Eastern. That 9 AM job that was supposed to run at 9 AM local time actually runs at 2 PM local time.
- Path assumptions. The script runs fine manually but fails in cron because cron has a minimal PATH. Always use absolute paths or set PATH explicitly in the crontab.
Cron Job Patterns for Production DevOps
These patterns are used in production environments:
Locking to prevent overlap:
*/5 * * * * flock -n /tmp/sync.lock /opt/scripts/sync-data.sh >> /var/log/sync.log 2>&1
Health check ping (dead man's switch):
0 2 * * * /opt/scripts/backup.sh && curl -s https://hc-ping.com/your-uuid > /dev/null
Staggered starts to avoid thundering herd:
# Stagger jobs across servers using different minute offsets 5 2 * * * /opt/scripts/server1-backup.sh 15 2 * * * /opt/scripts/server2-backup.sh 25 2 * * * /opt/scripts/server3-backup.sh
Environment-aware script header:
#!/bin/bash set -euo pipefail PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin LOG_FILE="/var/log/$(basename $0 .sh)-$(date +%Y%m%d).log" exec >> "$LOG_FILE" 2>&1 echo "=== Started at $(date) ==="Sell Custom Apparel — We Handle Printing & Free Shipping
When to Move Beyond Crontab to a Better Scheduler
Crontab is the right tool for many tasks. But consider upgrading when:
- You need distributed scheduling — jobs across multiple hosts need coordination. Look at: Celery (Python), Sidekiq (Ruby), Apache Airflow (data pipelines), or cloud-native schedulers.
- You need dependency ordering — Job B can only run after Job A completes successfully. Cron has no dependency graph. Airflow, Prefect, and Dagster handle this well.
- You need cross-timezone coordination — a job that needs to run at 9 AM in each user's timezone, not 9 AM server time. This is application logic, not cron.
- You need failure recovery — retry a failed job automatically with backoff. Cloud schedulers (AWS Step Functions, GCP Cloud Tasks) handle this; basic cron does not.
- More than 20-30 cron jobs — crontabs become hard to manage at scale. Tools like Ansible, Chef, or a centralized cron management system help.
Choosing the Right Schedule for Your Background Jobs
Match your schedule interval to the actual need, not a round number. "Every hour" sounds clean but may create unnecessary load during peak times:
- Backups: Off-peak windows only — 1 AM to 4 AM. Stagger across machines. One full backup per day; incrementals more frequently.
- Log rotation/cleanup: Daily, off-peak. Usually handled by logrotate — don't reinvent this with cron scripts.
- Health checks/monitoring: Every 1-5 minutes. Use a dedicated monitoring tool (Prometheus, Datadog) for high-frequency checks rather than cron.
- Data syncs: Match the frequency to how stale the data can be. Hourly syncs for most use cases; every 5 minutes only if you actually need near-real-time data.
- Reports/digests: Tie to business cadence — daily morning reports at 7 AM, weekly digests on Monday morning.
Build your schedule in our cron generator and verify with the visualizer before deploying. Catching a timezone mistake or off-by-one in the expression before it hits production is worth the 60 seconds.
Try It Free — No Signup Required
Runs 100% in your browser. No account, no install, no limits.
Open Free Cron GeneratorFrequently Asked Questions
How do I prevent cron jobs from running simultaneously (overlapping)?
Use flock for file-based locking: "flock -n /tmp/job.lock /path/to/script.sh". The -n flag causes flock to exit immediately if the lock is already held, so the second invocation skips instead of waiting. This is the standard Unix approach to preventing concurrent cron job execution.
How do I get notified when a cron job fails?
Add "|| notify-on-failure.sh" to your cron command, redirect stderr to a log file and monitor it, or use a dedicated cron monitoring service like Healthchecks.io or Cronitor. These services expect a "ping" from each successful job run and alert you if the ping doesn't arrive on schedule.
Should I use crontab or systemd timers for production DevOps?
Both work for production use. systemd timers have advantages: they integrate with systemd logging, can be managed with systemctl, support persistent timers that catch missed runs after downtime, and integrate with dependencies. Crontab is simpler and universally available. Teams comfortable with systemd often prefer timers; ops teams working across many distributions often prefer crontab for portability.

