Example of CRON
Cron examples are only useful if they are safe. This page provides real patterns you can copy, including logging and overlap protection.
- Use absolute paths.
- Redirect output to logs.
- Use
flockfor jobs that must not overlap. - Be explicit with destructive commands.
Run every 5 minutes
*/5 * * * * /usr/local/bin/check_status.sh >>/var/log/check_status.log 2>&1
Restart a service daily
0 3 * * * /usr/bin/systemctl restart apache2 >>/var/log/apache-restart.log 2>&1
Remove old logs weekly
find ... -delete permanently removes files.
Test the command without -delete first.
0 1 * * 0 /usr/bin/find /var/log -type f -name '*.log' -mtime +7 -delete >>/var/log/log-prune.log 2>&1
Run at reboot
@reboot /usr/local/bin/startup_script.sh >>/var/log/startup_script.log 2>&1
Daily backup job with lock
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
15 2 * * * flock -n /var/lock/wp-backup.lock /usr/local/bin/wp-backup-run >>/var/log/wp-backup.log 2>&1
Run on weekdays at 09:00 and 17:00
0 9,17 * * 1-5 /usr/local/bin/office_task.sh >>/var/log/office_task.log 2>&1
Monthly report on the first
0 2 1 * * /usr/local/bin/monthly_report.sh >>/var/log/monthly_report.log 2>&1
Confirm what is active
crontab -l || true
sudo crontab -l || true
systemctl list-timers --all | sed -n '1,120p'
Renew TLS certificates (certbot)
Many distros already install a systemd timer for certbot. If you schedule it manually, keep it quiet and log failures.
20 3 * * * /usr/bin/certbot renew --quiet >>/var/log/certbot-renew.log 2>&1
Database dump (daily)
30 2 * * * /usr/local/bin/daily-db-dump >>/var/log/db-dump.log 2>&1
Backup retention pruning (list then delete)
Run the list-only command first.
Then add -delete once you are sure the glob is correct.
0 4 * * * /usr/bin/find /backups -type f -name 'wp-*.tar.*' -mtime +30 -delete >>/var/log/backup-prune.log 2>&1
Offsite sync after backups
10 5 * * * /usr/bin/rclone copy /backups remote:wp-backups/site-a >>/var/log/offsite-copy.log 2>&1
Avoid thundering herd (random delay)
If you manage many servers, randomize start time so they do not all run at once.
0 2 * * * /bin/bash -lc 'sleep $((RANDOM % 300)); /usr/local/bin/job' >>/var/log/job.log 2>&1
Health check ping after success
Use a webhook-style health check service. This pattern pings only if the job exits successfully.
15 2 * * * /usr/local/bin/wp-backup-run && /usr/bin/curl -fsS https://hc.example/ping/uuid >/dev/null 2>&1
Disk usage report (daily)
0 6 * * * /bin/df -hT / >>/var/log/disk-usage.log 2>&1
Run a script on the first Monday
0 9 1-7 * 1 /usr/local/bin/monthly-first-monday-task >>/var/log/monthly-task.log 2>&1
Notes: make examples production-safe
- Prefer scripts in
/usr/local/binover long inline commands. - Always use absolute paths in cron.
- Add
flockfor long tasks. - Avoid running package upgrades from cron unless you accept surprise downtime.
If you need better reliability and logs, consider moving schedules to systemd timers.
Run a PHP script (explicit interpreter)
0 1 * * * /usr/bin/php -d detect_unicode=0 /var/www/html/wp-cron-runner.php >>/var/log/wp-cron-runner.log 2>&1
Run a heavy job with reduced priority
30 2 * * * /usr/bin/nice -n 10 /usr/bin/ionice -c2 -n7 /usr/local/bin/heavy-job >>/var/log/heavy-job.log 2>&1
Separate stdout and stderr logs
*/10 * * * * /usr/local/bin/job >>/var/log/job.out 2>>/var/log/job.err
Capture environment for debugging
If a job behaves differently under cron, capture the environment.
* * * * * /usr/bin/env | sort >>/var/log/cron-env.log
Dry-run destructive commands first
Example: delete old files.
0 1 * * 0 /usr/bin/find /var/log -type f -name '*.log' -mtime +7 -print >>/var/log/log-prune.dryrun.log 2>&1
When the output looks correct, change -print to -delete.
Troubleshooting examples
Job runs, but output is missing
- you forgot redirection
- cron mail is not configured
Fix by redirecting stdout/stderr.
Job runs twice
- overlapping schedule
- DST/timezone transition
Fix by adding flock and using UTC where appropriate.
Next steps
- Cron fundamentals:
opt/docker-data/apps/docusaurus/site/docs/server/linux-server/11-automation-task-execution/cron.mdx - Redirecting output:
opt/docker-data/apps/docusaurus/site/docs/server/linux-server/11-automation-task-execution/redirecting-output-and-logs.mdx