Cron Won't Warn You When It Fails: How to Monitor Scheduled Tasks

Automated English translation of the original Spanish article. Code examples keep their original identifiers.

Just Because Cron is Silent Doesn’t Mean Everything Works

Cron doesn’t know if your task finished successfully: it only knows it tried to run it. If your 3:00 AM backup fails, nobody gets an alert. The usual story is finding out weeks later, right when you need that backup and it doesn’t exist.

The problem isn’t cron. It does exactly what it promises. The problem is assuming that the absence of visible errors means everything went fine. In this article, we’ll look at how to move from blind faith to real monitoring with three small changes.

The Golden Rule

Cron only guarantees that it attempted to execute a task. Whatever happened next—whether the script finished, failed halfway through, or couldn’t even find a file—is beyond its scope and, by default, lost.

To achieve real monitoring, you need to apply three changes:

  1. Redirect output to a persistent destination.
  2. Make the script explicitly report success or failure, with readable timestamps.
  3. Configure an alerting channel that triggers only on failure, without relying on daily manual checks.

Each covers a different gap: the first preserves the evidence, the second makes it interpretable, and the third removes the need for someone to remember to check.

1. Redirect Output to a Persistent Destination

If you don’t redirect anything, whatever your script prints is lost. By default, cron attempts to send it via local mail to the user, and on many servers, that mail system is neither configured nor read by anyone. The solution is to pipe standard output and errors into a log file:

0 3 * * * /opt/scripts/respaldo.sh >> /var/log/respaldo.log 2>&1
  • >> appends to the end of the file instead of overwriting it, preserving history.
  • 2>&1 also sends errors (stderr) to the same file. Without this, the most critical information gets lost.
  • Ensure the user running the cron job has write permissions to that path, and rotate the log (for example, with logrotate) so it doesn’t grow indefinitely.

With this, you now have evidence. The next step is making it easy to interpret.

2. Make the Script Declare Success or Failure, with Date and Time

A log full of lines without timestamps or a clear verdict forces you to guess what happened. Ideally, the script itself should report when it started and whether it ended successfully or with an error, including a readable timestamp on each line:

#!/usr/bin/env bash
set -euo pipefail
log() { echo "$(date '+%Y-%m-%d %H:%M:%S') [$1] $2"; }
trap 'rc=$?; log ERROR "Falló en la línea $LINENO (código $rc)"; exit $rc'
ERR
log INFO "Inicio del respaldo"
pg_dump mibase > "/backups/mibase_$(date +%F).sql"
log OK "Respaldo completado"

On success, the log looks like this:

2026-11-09 03:00:01 [INFO] Inicio del respaldo
2026-11-09 03:00:14 [OK] Respaldo completado
  • set -euo pipefail ensures the script halts at the first error instead of continuing blindly.
  • trap ... ERR logs an [ERROR] line with the exact point of failure and preserves the exit code.
  • The YYYY-MM-DD HH:MM:SS format sorts naturally and is easy to search with grep.
  • The INFO, OK, and ERROR tags make the log easy for a human (or a tool) to filter in seconds.

3. An Alert That Only Fires on Failure

Checking logs every morning isn’t monitoring—it’s just another chore that will sooner or later be forgotten. Alerts should trigger automatically, and only when something goes wrong. A notification that arrives every day ends up being ignored.

Since the previous step already catches errors in a single place, you only need to add the alert dispatch inside the trap:

alerta() {
  curl -fsS -X POST -H "Content-Type: application/json" \
    -d "{\"text\":\"respaldo falló en $(hostname) a las $(date '+%F %T')\"}" \
    "$WEBHOOK_URL" || true
}

trap 'rc=$?; log ERROR "Falló en la línea $LINENO (código $rc)"; alerta; exit $rc' ERR

The channel itself doesn’t matter as much; choose one you actually look at:

  • A webhook to Slack, Discord, or Telegram.
  • An email using mail, if the server has outbound mail configured.
  • A push notification through a service like ntfy.

Store the webhook URL in an environment variable or in a file with restricted permissions, rather than hardcoding it into the script. The || true prevents a failure in sending the alert from masking the original error.

One case this doesn’t cover: if cron never runs the script in the first place (service down, syntax error in crontab), there won’t be a failure to report, and therefore no alert. For that scenario, heartbeat monitors (dead man’s switch) like Healthchecks.io exist: the script pings the service when it completes successfully, and the service alerts you if it stops receiving those pings.

Putting It All Together

These three changes fit into a single crontab line and a few lines of script. In the final script, the trap with alerting replaces the simple trap from step 2.

ChangeWhat it solvesHow it’s achieved
1. Persistent outputEvidence is no longer lost>> file.log 2>&1 in crontab
2. Explicit success or errorLogs are clear without guessingset -euo pipefail, trap ERR, and timestamps on every line
3. Alert only on failureNo manual checks neededWebhook, email, or push notification inside trap

Before considering a task monitored, test that it actually works: trigger an intentional failure (for example, point to a path that doesn’t exist) and verify that the [ERROR] line appears in the log and the alert arrives. An alert you’ve never seen fire is just an assumption.

Cron will remain silent, as always. The difference is that now, when something breaks, you’ll be the first to know—not the day you desperately need that backup.