TwitterIrcGateway 再起動スクリプト

TIGは息を吸うように使用しているのによくプロセス落ちたりCPUの使用率が100%越えてTLが流れなくなってしまい死んじゃうことがあるので、プロセスの監視とCPUの使用率のチェックをして再起動をかけるシェルスクリプトを書きました。

TwitterIrcGatewayCLI.exe の実行の部分は適当にオプション加えたりしてください。TwitterIrcGatewayのパスとも適当に直してください。再起動かかるごとにログを残すようにしてます。ロードアベレージが増えてるとかプロセスのCPU使用率がどのくらい上がったかとか、とりあえず原因究明な感じで。

とりあえず mono のせいかなーって思ったので最新の mono-2.4.2.3 入れて様子を見てみます。

#!/bin/bash

while true
do
#プロセスが落ちてたら再起動
   PID=`pgrep -f 'TwitterIrcGatewayCLI.exe'`
   if [ -z $PID ]; then
      cd /home/admin/TwitterIrcGateway
      mono TwitterIrcGatewayCLI.exe  &
      time=`date "+%Y/%m/%d %H:%M"`
      echo "restart $time" >> /home/admin/tigps.log
      echo `w | head -n 1 | awk -F"," '{print $4"," $5"," $6}'` >> /home/admin/tigps.log
   fi
#CPU使用率が90%越えたら再起動
   TIGCPU=`top -n 1 -b | grep mono | awk '{print $9}' | cut -d "." -f 1`
   if [ 90 -lt $TIGCPU ]; then
      kill -9 $PID
      sleep 3
      cd /home/admin/TwitterIrcGateway
      mono TwitterIrcGatewayCLI.exe  &
      time=`date "+%Y/%m/%d %H:%M"`
      echo "restart $time" >> /home/admin/tigps.log
      echo "tigcpu $TIGCPU" >> /home/admin/tigps.log
      echo `w | head -n 1 | awk -F"," '{print $4"," $5"," $6}'` >> /home/admin/tigps.log
      sleep 10
fi
   sleep 3
done