34
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

簡単なログ監視用のシェルスクリプト

Posted at

参考

http://lab.hde.co.jp/2008/04/tail.html
http://d.hatena.ne.jp/kakurasan/20100112/p1

log_checker.sh
#!/bin/sh

# メール送信設定
_to="nekogeruge_987@gmail.com"
_from="appname@gmail.com"

# メール件名設定
_hostname=`hostname`
_application="AppName"
_subject="Fatal error!!"
_title="[${_application}][${_hostname}]${_subject}"

# エラー条件
_error_conditions="ERROR"

# ログファイルをtailし、_error_conditionsの条件にヒットしたらメールを送信する
mail_alert() {
  while read i
  do
    echo $i | grep -q ${_error_conditions}
    if [ $? = "0" ];then
      echo $i | /usr/sbin/sendmail -t << EOF
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-2022-jp
From: ${_from}
To: ${_to}
Subject: ${_title}
Data Replication Failed

---

$i

---

Please check with a administrator for details. 

EOF
    fi
  done
}

tail -n 0 --follow=name --retry $1 | mail_alert

引数にログファイルを指定して実行する

$ sh log_checker.sh application.log
34
41
1

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
34
41

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?