Skip to content

Instantly share code, notes, and snippets.

@ianlini
Last active January 28, 2020 00:00
Show Gist options
  • Save ianlini/648943f4d0d61c58ac8b166ff3c8acb2 to your computer and use it in GitHub Desktop.
Save ianlini/648943f4d0d61c58ac8b166ff3c8acb2 to your computer and use it in GitHub Desktop.
Watch a log file and send a Slack message when there is new log.
#!/bin/bash
set -e
LOG_FILE='/var/log/motion/motion.log'
WEBHOOK_URL='https://hooks.slack.com/services/...'
USERNAME='motion'
CHANNEL='camera'
function send_slack_message {
DATA="payload={\"username\": \"${USERNAME}\", \"channel\": \"${CHANNEL}\", \"text\": \"$1\"}"
echo "data: ${DATA}"
curl -X POST --data-urlencode "${DATA}" "${WEBHOOK_URL}";
echo ""
}
send_slack_message 'motion alert started!'
tail -n0 -F "${LOG_FILE}" | while read LINE; do
send_slack_message "${LINE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment