· Back to the setup

AI Setups That Work

Alert when a 2 a.m. job fails

Researched

What it does

Get an email or text when a nightly job fails or never runs, by having Claude Code wrap the script and report to a heartbeat check.

Who it is for

Owners and one-person IT who run a nightly script on a Mac, Linux machine or server, such as a backup or export, and only learn it failed days later.

Tools

Steps

  1. Write down the job: the exact command, the time it runs, how long it normally takes and where its output goes now.
  2. In Healthchecks.io, create one check for this job. Set the schedule to match cron (a Cron schedule, with the expression from your crontab and your machine's time zone) and set the Grace Time longer than the job normally takes. Copy the check's ping URL.
  3. Add at least one notification method, and a second one if the job matters, such as email and a chat or text. Two channels give a second chance if one fails, but neither is a guarantee of delivery.
  4. Open Claude Code in a new folder in Manual mode and paste the prompt below with your command. Do not paste the ping URL into the chat or into any file that is shared; the prompt has you put it in an environment variable or a private file.
  5. Read what Claude Code wants to do and approve each action. It should write the wrapper and show you the cron line. It should not edit your crontab.
  6. Add the cron line yourself with crontab -e. Pick a time outside the hour when the clocks change.
  7. Test the failure alert. Run the wrapper by hand with a command that fails, such as false, and wait for the email.
  8. Test the missing-run alert. Leave the job off for one scheduled run and confirm the check goes late and alerts after the grace time.
  9. Put the check's name, the ping URL location and what to do when it alerts into one note, so whoever gets the alert knows the next step.

The prompt

I have a nightly job that runs from cron on this machine. Write a small shell script that wraps it and reports to Healthchecks.io.

The job command: /home/shop/bin/export-orders.sh (made-up sample).
Its schedule: 3:30 AM every day (a sample time chosen outside the hour when clocks change; check yours).
It normally takes a few minutes.

The wrapper must:
1. Send a "start" ping, run the job command, and save its output to a log file in a logs folder next to the script.
2. If the job exits with 0, send a success ping. If it exits with any other code, send a failure ping with the exit code, and keep the log.
3. Read the ping URL from an environment variable named HC_PING_URL, or from a private file, never from the script text. Never print the URL.
4. Use curl with a short timeout and a few retries, so a network problem does not hang the job. If the ping fails, write that to the log. Never delete or change the job's own output.
5. Exit with the job's own exit code.

Also:
- Show me the single crontab line to run the wrapper at the same time, loading the ping URL from the private file or environment, never written in the line. Do not edit my crontab yourself.
- Tell me how to test a failure on purpose and how to test a missed run.
- Do not change the job script, do not install anything and do not send anything except the pings. Before you run anything, say what you will do.

Tips

Sources