How to get an alert with the exit code when a cron job fails
Run your job, then call your ping URL with $? on the end. Exit code 0 records a success. Any other code records a failure and shows up in the alert.
You need: a Lateping account, shell access to the server, about 5 minutes.
The short answer
Run the job, then ping https://lateping.com/p/<check-id>/$?. The shell fills in $? with the job’s exit code. Code 0 records a success. Any other code from 1 to 255 records a failure, and the failure alert shows the code.
Why a plain success ping is not enough
A line like job.sh && curl https://lateping.com/p/<check-id> pings only when the job succeeds. If the job fails, nothing arrives, and you find out when the ping is late. That works, but you learn it late and you don’t learn why.
Sending the exit code closes that gap. A failure is reported as soon as the job ends, and the code tells you where to look: 1 is a general error, 126 is a command that could not run, 127 is a command not found, and 137 is a process killed with SIGKILL.
How to check what your job returns
Run the job by hand and print the code:
./scripts/backup.sh
echo "exit code: $?"
Do this as the same user cron runs it as. A job that exits 0 in your shell can fail under cron because of a different PATH or working directory.
The crontab line
Cron runs each command with /bin/sh unless the crontab sets SHELL. $? works in sh. Put this in crontab -e as one line:
17 2 * * * /home/deploy/scripts/backup.sh; curl -fsS -m 10 --retry 3 -o /dev/null https://lateping.com/p/<check-id>/$?
The ; runs the ping whether or not the job succeeded. With && the ping would run only on success and a failure would go unreported.
In a wrapper script
When the job has several steps, capture the code once and ping at the end:
#!/bin/sh
URL="https://lateping.com/p/<check-id>"
curl -fsS -m 10 --retry 3 -o /dev/null "$URL/start"
./scripts/backup.sh
status=$?
curl -fsS -m 10 --retry 3 -o /dev/null "$URL/$status"
exit "$status"
/start marks the beginning, so the check can measure run time. The last line passes the job’s own code back to cron, so cron’s logs and mail still see the failure.
What the alert shows
The code appears on the ping in the check’s Pings list, and in the failure alert by email, Slack and Discord. In a webhook it is alert.exit_code.
Notes and gotchas
- Range. Codes run from 0 to 255. A code over 255, or one with a sign, a leading zero or a decimal point, returns
404. The shell never produces those from$?, but a hand-built URL can. - Percent signs. Cron turns an unescaped
%in a command into a newline. Escape it as\%if your command needs one.$?has none. - Timeouts.
-m 10stops curl waiting more than 10 seconds, so a slow network can’t hold up the next job.--retry 3retries a failed ping. - GET or POST. Both work.
- The GitHub Action.
lateping/ping@v1doesn’t send exit codes. Use the/failURL there. - Alert timing. A failure alert goes out on the next scan, within about a minute.
Create a free check at https://app.lateping.com/login.