Last active
December 9, 2024 12:26
-
-
Save dmitrydyomin/88a68385e92414457b01fe6a5aec4013 to your computer and use it in GitHub Desktop.
Tail file and send lines to Telegram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# https://stackoverflow.com/a/48779000 | |
# https://dev.to/nathannosudo/bash-script-to-send-a-message-in-telegram-from-your-terminal-20c7 | |
if [ "$#" -ne 1 ]; then | |
echo "Usage: API_TOKEN=123 CHAT_ID=456 teletail /var/log/test.log" | |
exit 0 | |
fi | |
if [ ! -f "$1" ]; then | |
echo "File $1 not found!" | |
exit 1 | |
fi | |
process_new_line() { | |
local var="" #declare some local variables here | |
read | |
while true | |
do | |
#process $REPLY | |
#The new line content is in the variable $REPLY | |
#store the result in the local variable according to your rules. | |
curl -s -X POST https://api.telegram.org/bot$API_TOKEN/sendMessage -d chat_id=$CHAT_ID -d text="$REPLY" | |
read | |
done | |
} | |
tail -n0 -f "$1" | process_new_line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
systemd service example: