Skip to content

Instantly share code, notes, and snippets.

@rikwatson
Created November 26, 2016 15:05
Show Gist options
  • Save rikwatson/b42b0f66a36e3df52196afc50c878919 to your computer and use it in GitHub Desktop.
Save rikwatson/b42b0f66a36e3df52196afc50c878919 to your computer and use it in GitHub Desktop.
Stream any log file to Slack using curl
#!/bin/bash
tail -n0 -F "$1" | while read LINE; do
(echo "$LINE" | grep -e "$3") && curl -X POST --silent --data-urlencode \
"payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2";
done
@rikwatson
Copy link
Author

Usage

./tail-slack.sh "file.log" "https://hooks.slack.com/services/...";
./tail-slack.sh "/var/log/ngix/access.log" \
  "https://hooks.slack.com/services/..." " 404 ";

Don't forget nohup so will run post logout from ssh or whatever.

#ios_notifications webhook (It's in 1password, Rik)

See here to modify.

@armellarcier
Copy link

echo "$LINE" instead of echo $LINE will prevent * characters to output the list of files in the cwd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment