Get instantly notified via Telegram when your cron task fails

In your crontab, define a couple of variables (put them before cron rules):

ERROR_TELEGRAM_CHAT_ID="put your chat id here"
ERROR_TELEGRAM_TOKEN="put your token here"

Create a script and put it somewhere, for instance ~/.local/bin/tgalert or whereever you like. Make sure curl is installed or use some alternative.


die() {
    >&1 echo "error: $@"
    exit 1


[ -z "$token" ] && die "no token"
[ -z "$chat_id" ] && die "no chat_id"

curl -X POST -F "chat_id=${chat_id}" -F "text=$1" "$url"

Then in your cron rule:

0 * * * * /home/user/ >/dev/null || ~/.local/bin/tgalert "cron error: very_important_script failed!"
If you have any comments, contact me by email.