Skip to content

Writing safe crontab entries

Use absolute paths, redirect output to a log, set PATH explicitly, and lock overlapping runs.

Writing a crontab that survives

  1. Absolute paths

    cron has a minimal PATH. /usr/local/bin/backup.sh, not backup.sh.

  2. Redirect output

    cron mails stdout/stderr to the owner. On a modern system without a local MTA, that mail is lost. Redirect explicitly.

  3. Set the environment

    PATH, HOME, MAILTO, and any variables your script needs. cron does not load your .bashrc.

  4. Lock overlapping runs

    Use flock -n /var/lock/backup.lock so a long-running job does not start twice.

  5. Test the schedule

    Use crontab.guru or a dry-run to verify the five fields. Off-by-one errors in cron are the most expensive kind.