Get instantly notified via Telegram when your cron task fails
7 November 2022
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.
#!/bin/bash
die() {
>&1 echo "error: $@"
exit 1
}
token="$ERROR_TELEGRAM_TOKEN"
chat_id="$ERROR_TELEGRAM_CHAT_ID"
[ -z "$token" ] && die "no token"
[ -z "$chat_id" ] && die "no chat_id"
url="https://api.telegram.org/bot${token}/sendMessage"
curl -X POST -F "chat_id=${chat_id}" -F "text=$1" "$url"
Then in your cron rule:
0 * * * * /home/user/very_important_script.sh >/dev/null || ~/.local/bin/tgalert "cron error: very_important_script failed!"
If you have any comments, contact me by email.