Skip to content

Hardened Cron Wrapper Generator

Quick Answer

Cron jobs fail in three quiet ways: they overlap when a run takes longer than its interval; they hang when a command blocks on a dead socket or lock and never exits; and they die on a transient blip that would have succeeded on a second try. This tool composes the three guards: flock holds a kernel lock so only one copy runs at a time (and the kernel frees it on crash or kill, no stale PID files), timeout sends SIGTERM at a deadline and SIGKILL after a grace period for processes stuck in uninterruptible I/O, and a retry loop with exponential backoff and jitter survives a flaky network or a not-yet-ready dependency without hammering the recovering service. Toggle any combination, set the schedule, and get a ShellCheck-clean wrapper script and the crontab line that calls it.

How to use the Hardened Cron Wrapper Generator

  1. 1Enter the command your cron job runs in the Command field. For a pipeline or multiple commands, enter bash -c 'cmd1 && cmd2'.
  2. 2Toggle Lock (flock) to prevent overlapping runs. Choose Skip if busy for frequent jobs where a missed run is harmless, or Wait then give up with a timeout for jobs that must eventually run.
  3. 3Toggle Bound the runtime (timeout) and set Max runtime to longer than the job's normal worst case but well under its cron interval. Set SIGKILL grace to catch processes stuck in I/O.
  4. 4Toggle Retry transient failures to survive a blip or a not-yet-ready dependency. Set max attempts and base delay — the backoff doubles each round up to a 30-second cap.
  5. 5Toggle Timestamped logging to send output to a file instead of cron's mail. Set the log path or leave it blank to auto-generate from the job name.
  6. 6Toggle Email alert on failure to send mail via mailx when the job fails after all retries. Requires mailx configured on the host.
  7. 7Set the Cron schedule or build the expression in the Cron Job Builder tool.
  8. 8Copy the wrapper tab for the full bash script — install it with sudo install -m 755. Copy the crontab tab for the line to paste into crontab -e.

Run this script on a real Linux server

Get $200 free credit — DigitalOcean

Get $200 Free →

Affiliate link · we earn a commission

Need a domain for your next project?

Register with Namecheap — free WHOIS privacy included

Check Domain Prices →

Affiliate link · we earn a commission

PAID RESOURCE — $9

The Production Bash Toolkit

6 scripts + shared library + 52-page field guide. The production layer the free snippets don't cover.

Get the Toolkit →

Frequently Asked Questions

faq — tool

What is the difference between this and the Cron Job Builder?

The Cron Job Builder builds the cron schedule expression — the five-field timing string. This tool builds the wrapper script that runs when the schedule fires. They're designed to be used together: build the schedule there, build the hardened script here, paste both into crontab -e.

faq — tool

Why does the crontab line change when I turn on retry or alerting?

Lock and timeout are simple enough to inline directly on the crontab line — a one-liner with flock and timeout in front of the command. Retry and email alert require a bash function and mailx call, which can't fit on a crontab line. When either is on, the output switches to a wrapper script and a crontab line that calls it.

faq — tool

What does SIGKILL grace mean?

timeout first sends SIGTERM — a polite signal a well-behaved program uses to clean up and exit. But a process blocked in uninterruptible I/O (disk or NFS) physically cannot act on SIGTERM. SIGKILL grace is how long timeout waits after SIGTERM before sending SIGKILL, which the kernel enforces unconditionally. Set it to 15–30 seconds.

faq — tool

Should I use Skip if busy or Wait for the lock mode?

Skip if busy (-n) is right for frequent cron jobs where a missed run is harmless — a metrics push, a sync that catches up next minute. Wait then give up (-w) is for jobs that must run eventually but can tolerate a short queue. Never leave the lock in blocking mode with no timeout on a frequent schedule.

faq — tool

Where should the lock file live?

/run/lock is the right location on any modern systemd system — it is a tmpfs cleared cleanly on reboot, so you never inherit a stale lock across a reboot. The tool generates the lock path from the job name automatically. Avoid /tmp: systemd-tmpfiles periodically deletes old files there and can delete a lock mid-run.

Related Snippets