Minecraft サーバの監視再起動

Minecraft サーバが OutOfMemory で動作しなくなる前に、一定量のメモリを超過したらログインユーザーに再起動を通知し、サーバを再起動するスクリプト。screen で動かしておく。java プロセスが他にもあるとダメ。

#!/bin/bash
procname="jav[a]"
IFS=' '
set -e

while true
do
        echo "FINDING java PROCESS ..."
        wcout=$( ps aux | grep $procname | wc | awk '{sum += $1}END{print sum}')
        echo "$wcout PROCESSES FOUND."
        if [[ "$wcout" -eq "0" ]]; then
                echo "START MINECRAFT SERVER"
                if [ ! -p in ]; then
                        mkfifo in
                fi
                tail -f in | java -Xms256M -Xmx1024M -jar server.jar nogui &
                sleep 10
        else
                memu=$(ps aux | grep $procname | awk '{sum += $6}END{print sum}')
                if [[ "$memu" -gt "720000" ]]; then
                        pid=$(ps | grep $procname | awk '{sum += $1}END{print sum}')
                        echo "say [WARNING] Minecraft Server will be rebooted in 30 seconds." > in
                        sleep 15
                        echo "say [WARNING] Minecraft Server will be rebooted in 15 seconds." > in
                        sleep 5
                        echo "say [WARNING] Minecraft Server will be rebooted in 10 seconds." > in
                        sleep 5
                        echo "say [WARNING] Minecraft Server will be rebooted in 5 seconds." > in
                        sleep 1
                        echo "say [WARNING] Minecraft Server will be rebooted in 4 seconds." > in
                        sleep 1
                        echo "say [WARNING] Minecraft Server will be rebooted in 3 seconds." > in
                        sleep 1
                        echo "say [WARNING] Minecraft Server will be rebooted in 2 seconds." > in
                        sleep 1
                        echo "say [WARNING] Minecraft Server will be rebooted in 1 seconds." > in
                        sleep 1
                        echo "stop" > in
                        sleep 15
                        tailpid=$(ps | grep "tai[l]" | awk '{sum += $1}END{print sum}')
                        kill $tailpid
                fi
        fi
        sleep 3
done