Help us understand the problem. What is going on with this article?

サーバーが落ちたら自動的に再起動するシェルスクリプト

More than 3 years have passed since last update.

よくJavaのプロセスとかが死にやすい場合に、仕方がなく設定するスクリプト、死活監視をして問題あらば再起動する。

monitor_svr.sh
#!/bin/sh
monitorInterval=3
bootWait=10
partOfProgramName="minecraft_server"
while true
do
  isAlive=`ps -ef | grep $partOfProgramName | grep -v grep | wc -l`
  if [ $isAlive = 1 ]; then
    echo "[起動中]: $partOfProgramName `date`"
  else
    echo "[停止]: $partOfProgramName `date`"
    flag=true
    while $flag
    do
      echo "[再起動中]: $partOfProgramName"
      isRebooted=`ps -ef | grep $partOfProgramName | grep -v grep | wc -l`
      if [ $isRebooted = 1 ]; then
        flag=false
      fi
      sleep $bootWait
      /usr/bin/java -Xmx840M -Xms840M -jar minecraft_server.1.10.jar nogui & >/dev/null 
    done
  fi
  sleep $monitorInterval
done

プログラムと同じフォルダに入れていいて一緒に走らせておくと自動復旧する。

用途はAWSのt2.microみたいな貧弱なサーバーにminecraftのサーバー立てたりする時に使う。

それでもダメなときは、

restart_svr.sh
#!/bin/sh
bootWait=10
partOfProgramName="minecraft_server"
isAlive=`ps -ef | grep $partOfProgramName | grep -v grep | wc -l`
if [ $isAlive = 1 ]; then
  echo "[起動中]: $partOfProgramName `date`"
else
  echo "[停止]: $partOfProgramName `date`"
  flag=true
  while $flag
  do
    echo "[再起動中]: $partOfProgramName"
    isRebooted=`ps -ef | grep $partOfProgramName | grep -v grep | wc -l`
    if [ $isRebooted = 1 ]; then
      flag=false
    fi
    sleep $bootWait
    /usr/bin/java -Xmx840M -Xms840M -jar minecraft_server.1.10.jar nogui & >/dev/null
  done
fi

こいつをcrontabで登録して実行する。ここまでやっておくとなかなか死なない。なおrestart_svr.sh まで登録してしまうと逆にシャットダウンする時は、cronを止めとく必要があることを忘れないように。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした