LoginSignup
20
15

More than 5 years have passed since last update.

tailコマンドでログを監視して、slackにアラート投げる

Posted at

時間がかかるバッチを叩いて終わった時に通知が欲しいと思ったので、
なんかないかなと思ったら以下の記事を発見。

ERRORログをメールで通知する ~tailコマンド編~

記事中のスクリプトで mail コマンド叩いているところを
slack の Incoming webhook に変更

slack_alert.sh
#!/bin/sh

slack_alert() {
  while read i
  do
    echo $i | grep -q "Finish."
    if [ $? = "0" ];then
        curl -X POST -H 'Content-type: application/json' \
        --data '{"text":"Finish!!"}' \
        https://hooks.slack.com/services/{Incoming Webhook URL}
    fi
  done
}

tail -n 0 --follow=name --retry $1 | slack_alert
$ ./slack_alert.sh output.log

logwatch とかだと重すぎるってときとかにさらっと使えてよさげ。

20
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
15