キモブロ

Please spy check please, Fucking retard

サーバーを再起動しても自動でSRCDSが起動するようにする

CS:GO wikiにあった/etc/init.d/srcdsをTF2向けに編集したバージョンを作った。しかし日本語だといろいろ問題ありそうだし、英語オンリーのバージョンも貼っといたほうがいいんじゃないのかなぁ。
http://wikiwiki.jp/csgo/?%A5%B5%A1%BC%A5%D0%A1%BC%A4%CE%B7%FA%A4%C6%CA%FD

オリジナルの英語版をどっかで探してきて後でそれに差し替える予定。

#! /bin/bash
#
# Start the Counter-Strike: Source dedicated server.
#
# AUTHORS :
#
# Julien Escario ( pandemik@azilog.net )
# &
# Cedric Rochat ( crochat@younics.org )
# &
# Cmdr._Firewalker ( cmdr._firewalker@web.de )
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# chkconfig: 2345 44 56
# Description: srcds
#
# ===========================================
#
# What you need:
#
# Linux :)
# awk
# screen
# the srcds_l & cstrike files (obtainable via steam)
#
# How to use:
#
# Edit the CS_USER-Var to the user running your cs-servers
# Edit the DIR-Var to fit your system (just contains the path to the dir that contains srcds_run)
# Edit the PARAMS-Var to fit your needs
# - standard is startup as internet server
# if you use multiple scripts on one system you should change the NAME-var (otherwise 
#   all will be stopped if you run the script with "stop", and status will be useless)
#
# When this is done, copy the file to /etc/rc.d/init.d (or whereever your system stores the
# scripts for starting the services) and set the execute permission.
# Now you can link the script to your runlevel-dir, here's an example for runlevel 3:
# ln -s /etc/rc.d/init.d/srcds /etc/rc.d/rc3.d/S90srcds
# ln -s /etc/rc.d/init.d/srcds /etc/rc.d/rc3.d/K50srcds
# or use update-rc.d if you got a system using that tool (like Debian, see manpage)
#
# Or use it manually like:
# ./srcds start
# ./srcds stop
#
# To see the server-console start the script with the parameter "watch".
#
# DOC by jwm (jwm@counter-strike.de)
#
# 12/08/25
# chkconfig 対応 & 日本語訳
# by niu
# !!!!!!!!!!!!!! CHANGE THIS TO THE USER YOU USE FOR YOUR SERVER !!!!!!!!!!!!!!!
# 実行するユーザー名8
CS_USER=kimoto

PATH=/bin:/usr/bin:/sbin:/usr/sbin

# DON'T FORGET TO CHANGE THE PATH TO YOUR NEEDS!
DIR=/home/kimoto/srcds/tf/orangebox

DAEMON=srcds_run

# LAN server:
# PARAMS="-game cstrike -nomaster -insecure +sv_lan 1 +maxplayers 16 +map de_dust"
# Internet server:
PARAMS="-game tf +randommap -condebug +maxplayers 32 -autoupdate +ip 0.0.0.0 +mapcyclefile \"mapcycle_mvm.txt\" $CS_USER"

NAME=tf2
DESC="Team Fortress 2 Server"

# No edits (should be) necessary beyond this line

if [ ! -x `which awk` ]; then echo "このスクリプトにはawkが必要です"; exit 1; fi
if [ ! -x `which screen` ]; then echo "このスクリプトにはscreenが必要です"; exit 1; fi

if [ `whoami` = root ]
then
 usagetype=root
else
 usagetype=nonroot
fi

start() {
   if [ ! -d $DIR ]; then echo "ディレクトリが見つかりません: $DIR!";  exit 1; fi
   if [ ! -x $DIR/$DAEMON ]; then echo "$DIR/$DAEMON が見つからないか実行出来ません。"; exit 1; fi
   if status; then echo " ... $DESC: $NAME は既に実行されています。"; exit 1; fi

   case "$usagetype" in
   root)
    su - $CS_USER -c "cd $DIR ; screen -AmdS $NAME ./$DAEMON $PARAMS"
   ;;
   nonroot)
    cd $DIR ; screen -AmdS $NAME ./$DAEMON $PARAMS
   ;;
   esac

}

stop () {
   if ! status; then echo " ... $DESC $NAME が見つからないか、恐らく起動していません。"; exit 1; fi

   case "$usagetype" in
   root)
    mp=$(su - $CS_USER -c "screen -ls" | awk -F . "\$2 ~ /^$NAME[[:space:]].*/ {print \$1}" | awk '{print $1}')
    su - $CS_USER -c "screen -r $tmp -X quit"
   ;;
   nonroot)
    screen -r $(screen -ls | awk -F . "\$2 ~ /^$NAME[[:space:]].*/ {print \$1}" | awk '{print $1}') -X quit
   ;;
   esac
}

status () {
   case "$usagetype" in
   root)
    su - $CS_USER -c "screen -ls" | grep [.]$NAME[[:space:]] > /dev/null
   ;;
   nonroot)
    screen -ls | grep [.]$NAME[[:space:]] > /dev/null
   ;;
   esac
}

watch () {
   if ! status; then echo "$DESC $NAME が見つからないか、恐らく動作していません"; exit 1; fi

   case "$usagetype" in
   root)
    tmp=$(su - $CS_USER -c "screen -ls" | awk -F . "\$2 ~ /^$NAME[[:space:]].*/ {print \$1}" | awk '{print $1}')
    su - $CS_USER -c "screen -r $tmp"
   ;;
   nonroot)
    screen -r $(screen -ls | awk -F . "\$2 ~ /^$NAME[[:space:]].*/ {print \$1}" | awk '{print $1}')
   ;;
   esac
}

case "$1" in
 start)
  echo "起動中 $DESC: $NAME"
  start
  echo " ... 完了!"
 ;;

 stop)
  echo "停止中 $DESC: $NAME"
  stop
  echo " ... 完了!"
 ;;

 restart)
  echo "再起動中 $DESC: $NAME"
  status && stop
  start
  echo " ... 完了!"
 ;;

 status)
  if status
  then echo "$DESC: $NAME は起動しています"
  else echo "$DESC: $NAME は停止しています"
  fi
 ;;
 watch)
  watch
 ;;

 *)
   echo "使い方: $0 {start|stop|status|restart|watch}\nwatch から戻る際は ctrl-a,d と入力して下さい"
   exit 1
   ;;

esac

exit 0

/etc/init.d/srcdsの配備

sudo chkconfig srcds on
sudo chkconfig srcds --list
sudo shutdown -r now 

で再起動、自動起動していることを確認する

これをやっていたらSushi鯖の#1と#3だけ自動で復旧しなかった。どうも新しいバージョンのカーネルのデフォルト設定だと、VPS向けの設定がちゃんとなされていなくて、それをやらんと駄目っぽい。カーネルのバージョンを元のに戻したらちゃんと動いた。たぶんシリアルポートあたりの設定をしなければならない感じ。

デフォルトで起動するカーネルを選択するには、/boot/grub/menu.lstを編集して、default=0 ってなってるところを好きなやつに変えればOK.
俺は上から2個目のカーネルを自動選択肢たかったのでdefault=1とした(0から採番されることに注意)


このやり方の問題点。

  • srcdsがフリーズした時にそれを検知できずにずっと落ちたままになること
  • またCS:GOのwikiだとcrontabで毎日再起動させてるけど、その時間にプレイしてた人たちがかわいそうだし、sv_shutdownをつかうか、自作のスクリプトかなんかで人が居ない時だけ終了させたほうが良い。