Skip to main content

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.

Quick Summary
  • Use absolute paths.
  • Redirect output to logs.
  • Use flock for jobs that must not overlap.
  • Be explicit with destructive commands.

Run every 5 minutes

cron-every-5-minutes.txt
*/5 * * * * /usr/local/bin/check_status.sh >>/var/log/check_status.log 2>&1

Restart a service daily

cron-restart-service-daily.txt
0 3 * * * /usr/bin/systemctl restart apache2 >>/var/log/apache-restart.log 2>&1

Remove old logs weekly

warning

find ... -delete permanently removes files. Test the command without -delete first.

cron-delete-old-logs-weekly.txt
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

cron-at-reboot.txt
@reboot /usr/local/bin/startup_script.sh >>/var/log/startup_script.log 2>&1

Daily backup job with lock

cron-backup-with-flock.txt
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

cron-weekdays-twice-daily.txt
0 9,17 * * 1-5 /usr/local/bin/office_task.sh >>/var/log/office_task.log 2>&1

Monthly report on the first

cron-monthly-first-day.txt
0 2 1 * * /usr/local/bin/monthly_report.sh >>/var/log/monthly_report.log 2>&1

Confirm what is active

list-crontab-and-timers.sh
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.

cron-certbot-renew.txt
20 3 * * * /usr/bin/certbot renew --quiet >>/var/log/certbot-renew.log 2>&1

Database dump (daily)

cron-db-dump-daily.txt
30 2 * * * /usr/local/bin/daily-db-dump >>/var/log/db-dump.log 2>&1

Backup retention pruning (list then delete)

warning

Run the list-only command first. Then add -delete once you are sure the glob is correct.

cron-prune-backups.txt
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

cron-offsite-rclone-copy.txt
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.

cron-random-delay-example.txt
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.

cron-job-with-healthcheck-ping.txt
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)

cron-disk-usage-report.txt
0 6 * * * /bin/df -hT / >>/var/log/disk-usage.log 2>&1

Run a script on the first Monday

cron-first-monday.txt
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/bin over long inline commands.
  • Always use absolute paths in cron.
  • Add flock for 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)

cron-run-php-script.txt
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

cron-run-heavy-job-with-nice-ionice.txt
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

cron-separate-stdout-stderr.txt
*/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.

cron-capture-env.txt
* * * * * /usr/bin/env | sort >>/var/log/cron-env.log

Dry-run destructive commands first

Example: delete old files.

cron-find-dry-run.txt
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