AlphornAlphorn Docs

Cron Job & Script Monitoring

Know when your scheduled scripts succeed or fail.

Cron jobs and batch scripts run silently. When they fail, nobody notices until something breaks downstream. Wrap your scripts with a notification call and route results through Alphorn.

Basic pattern

Send a notification at the end of any script based on its exit code:

#!/bin/bash
WEBHOOK="https://app.alphorn.dev/api/webhooks/wh_abc123"
JOB_NAME="nightly-data-sync"

# Your actual work
/usr/local/bin/sync-data.sh
EXIT_CODE=$?

if [ $EXIT_CODE -eq 0 ]; then
  curl -s -X POST "$WEBHOOK" \
    -H "Content-Type: application/json" \
    -d "{
      \"title\": \"$JOB_NAME completed\",
      \"message\": \"Job finished successfully\",
      \"priority\": 2,
      \"tags\": [\"cron\", \"success\"]
    }"
else
  curl -s -X POST "$WEBHOOK" \
    -H "Content-Type: application/json" \
    -d "{
      \"title\": \"$JOB_NAME FAILED\",
      \"message\": \"Job exited with code $EXIT_CODE\",
      \"priority\": 5,
      \"tags\": [\"cron\", \"failure\"]
    }"
fi

Reusable wrapper

Create a generic notification wrapper you can use with any command:

notify-run.sh
#!/bin/bash
WEBHOOK="https://app.alphorn.dev/api/webhooks/wh_abc123"
JOB_NAME="${1:-unknown-job}"
shift

# Run the command
"$@"
EXIT_CODE=$?

if [ $EXIT_CODE -eq 0 ]; then
  PRIORITY=2; STATUS="completed"; TAG="success"
else
  PRIORITY=5; STATUS="FAILED (exit $EXIT_CODE)"; TAG="failure"
fi

curl -s -X POST "$WEBHOOK" \
  -H "Content-Type: application/json" \
  -d "{
    \"title\": \"$JOB_NAME $STATUS\",
    \"message\": \"Command: $*\",
    \"priority\": $PRIORITY,
    \"tags\": [\"cron\", \"$TAG\"]
  }"

exit $EXIT_CODE

Usage:

# In crontab
0 2 * * * /path/to/notify-run.sh "db-backup" pg_dump -U app mydb > /backups/nightly.sql
0 4 * * * /path/to/notify-run.sh "log-rotation" /usr/sbin/logrotate /etc/logrotate.conf

Only notify on failure

If you don't want noise from successful runs, just skip the success notification — or use filtering rules in Alphorn to only route failure tagged messages to noisy channels like Slack, while sending everything to Email for audit.

Routing examples

ChannelFilterPurpose
Slack (#ops)tags CONTAINS "failure"Only failures
Emailtags CONTAINS "cron"Full log of all job runs
PagerDutypriority >= 4 AND tags CONTAINS "failure"Wake someone up for critical job failures

On this page