Ping Clock Ping Clock

Crontab generator

20 checks free.

Build a crontab line

Choose when the job should run, type the command, tick the options you want, and copy the finished crontab line. The generator handles the fiddly parts: absolute paths, output redirection, overlap protection and a monitoring ping.

What a good crontab line looks like

A bare line works until it doesn't:

0 2 * * * backup.sh

A line that survives contact with reality:

0 2 * * * /usr/bin/flock -n /tmp/backup.lock /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1 && curl -fsS -m 10 --retry 5 https://www.pingclock.com/ping/YOUR-CHECK-ID

Here's what each piece does, and why the generator can add it for you.

Absolute paths

Cron runs with a minimal environment. Your interactive shell's PATH isn't there. backup.sh might not be found; /usr/local/bin/backup.sh will be. The same goes for commands inside the script. Either use full paths or set PATH at the top of the crontab.

Output redirection

>> /var/log/backup.log 2>&1 appends standard output and errors to a log file. Without it, cron tries to email output to the local user, which on most servers goes nowhere.

Overlap protection

flock -n /tmp/backup.lock stops a second copy starting if the first is still running. Long jobs that overlap can corrupt data or exhaust resources.

The ping

&& curl ... only runs if the job exited with 0. If the job fails, no ping, and Ping Clock alerts you when the expected ping doesn't arrive. -fsS keeps curl quiet unless something goes wrong, -m 10 caps it at 10 seconds, and --retry 5 handles a brief network blip.

Variations

Report failures explicitly instead of waiting for the grace period to run out:

/usr/local/bin/job.sh; curl -fsS -m 10 https://www.pingclock.com/ping/YOUR-CHECK-ID/$?

$? is the exit code. A non-zero code marks the run as failed straight away.

Track duration by pinging at the start too:

curl -fsS -m 10 https://www.pingclock.com/ping/YOUR-CHECK-ID/start && /usr/local/bin/job.sh && curl -fsS -m 10 https://www.pingclock.com/ping/YOUR-CHECK-ID

Installing the line

  1. Run crontab -e as the user that should own the job.
  2. Paste the line at the bottom.
  3. Save and exit. Most systems print "installing new crontab".
  4. Check with crontab -l.

For system-wide jobs in /etc/crontab or /etc/cron.d/, there's an extra field for the user between the schedule and the command. The generator has a toggle for that format.

Common mistakes

  • Editing /etc/crontab with crontab -e syntax, or the other way around
  • Forgetting the trailing newline in files under /etc/cron.d/ (some cron versions ignore the last line without it)
  • Using % in commands without escaping it as \%; cron treats % as a newline
  • Testing the command as root, then scheduling it as another user who lacks permissions

Questions

Does this work on macOS?

macOS still supports cron, though launchd is the preferred scheduler. The line format is the same.

Do I need flock?

Only for jobs that could run longer than their interval. It's cheap insurance.

Create a free check