---
name: get-an-alert-when-a-2-am-job-fails-with-claude-code
description: "Alert when a 2 a.m. job fails. Have Claude Code write a small wrapper around the nightly job. The wrapper runs the job, then pings a monitoring check: one ping for success, another for failure. If."
---

# Alert when a 2 a.m. job fails

## When to use

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.

## 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.

## Prompt

Fill the [BRACKETED] parts with the user, then follow this 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.
```

## Guardrails

- Draft first and show the user. Never send, post, pay, book, cancel or delete without the user's explicit approval of that exact action.
- Use only facts the user gives you or that you can cite. If something is missing, ask instead of guessing.

## Common snags

- The cron job has a different PATH or environment from your shell. Use full paths in the wrapper and the cron line.
- The success ping fires even though the job half-failed. The job itself has to exit with a non-zero code when it fails, so test that.
- No alert arrives. Check the notification method on the check, your spam folder and that the check is not Paused.
- The check is always late by an hour. The time zone on the check does not match the machine.
- The machine is off or asleep at night. The check will alert, which is the point. Run the job on a machine that stays on.

## Source

https://www.aisetupsthatwork.com/setups/get-an-alert-when-a-2-am-job-fails-with-claude-code/ (AI Setups That Work)
