[mohaa] Server restart script

|TF20|Shockwave shockwave at clan-tf20.com
Tue Aug 17 14:31:24 EDT 2004


> > does anyone know of a script that can restart a server when it crashes
BUT
> > it leaves the procces running, i think it might be what your talking
> > about,


It's been a while since I ran one of these servers but I found something
that might help you.  I used to run mine using two steps.  This first one
launches the script that does the work in a separate screen:

----------------------

#!/bin/bash

# set variables
PROCESS_NAME="mohaa_auto"
RESTART_SCRIPT="mohaa"

# print header
echo
echo "MOH:AA Dedicated Server Launcher"
echo "==================================="

echo "Launching automatic restart script..."

# start automatic restart script
screen -S $PROCESS_NAME -m -d $RESTART_SCRIPT

echo "Started server screen in background named: $PROCESS_NAME"
echo

----------------------

This is the script that does all the work:

----------------------

#!/bin/bash

# set variables
PROG_NAME="path_to_binary"                    # tool program name
SCREEN_NAME="mohaa"                     # name of the tool screen
LOG_DIR="$HOME/.mohaa/main/"            # path to the log file directory
GAME_LOG_NAME="ragnarok.log"            # name of game server log
STATS_LOG_NAME="stats.log"              # name of statistics log
GAME_LOG_SUFFIX=".glog"                 # game log archive suffix
AUTOSTART_LOG_NAME="autostart.log"      # name of autostart script log
WAIT_TIME=5                             # time to wait before restarting
MIN_LIFESPAN=10                         # min time (sec) for good server
start
FAIL_LIMIT=5                            # threshold for script abort
FAIL_MSG="WARNING: Server start failed too quickly!"
ABORT_MSG="Too many server start failures, aborting..."

# start main loop
FAILURES=0              # reset server start failure count
while true
 do

  # save last copy of log files
  echo
  echo "Archiving log files..."

  # prime date variable CCYYMMDDHHMMSS
  STAMP="`date +%Y%m%d%H%M%S`"

  echo "Saving game log..."
  cp $LOG_DIR$GAME_LOG_NAME $LOG_DIR$STAMP$GAME_LOG_SUFFIX

  # add data to statistics log file
  echo "Appending game data to statistics log file..."
  cat $LOG_DIR$GAME_LOG_NAME >> $LOG_DIR$STATS_LOG_NAME

  # truncate log
  echo "Truncating existing log file..."
  > $LOG_DIR$GAME_LOG_NAME

  # record server start time
  START="`date +%Y%m%d%H%M%S`"
  echo "Server started: `date`" >> $LOG_DIR$AUTOSTART_LOG_NAME

  # start server
  echo "Starting game server..."
  screen -S $SCREEN_NAME $PROG_NAME
  echo

  # record server stop time
  STOP="`date +%Y%m%d%H%M%S`"
  echo "Server stopped: `date`" >> $LOG_DIR$AUTOSTART_LOG_NAME
  echo "Server terminated.  Press <Ctrl><C> to terminate automatic
restart..."

  # calculate server lifetime and determine appropriate action
  DIFF="`expr $STOP - $START`"
  if test $DIFF -lt $MIN_LIFESPAN;
   then
    # server wasn't alive long enough
    echo "$FAIL_MSG"
    echo "$FAIL_MSG" >> $LOG_DIR$AUTOSTART_LOG_NAME
    FAILURES=`expr $FAILURES + 1`
    if test $FAILURES -ge $FAIL_LIMIT;
     then
      echo "$ABORT_MSG"
      echo "$ABORT_MSG" >> $LOG_DIR$AUTOSTART_LOG_NAME
      exit 1   # bail out
    fi
   else
    FAILURES=0
    echo "Restarting in $WAIT_TIME seconds..."
    echo >> $LOG_DIR$AUTOSTART_LOG_NAME
    sleep $WAIT_TIME
  fi

 done

----------------------

I had to take out some of my extra stuff because I used to use it for
testing my server admin tool, but this should do the trick.  It launches the
server binary with some protection built into the script if it fails to stay
active.  If it doesn't remain up for MIN_LIFESPAN, it will restart for a
maximum of FAIL_LIMIT before the script dies.  Of course, you can adjust the
variables at the top to meet your needs.  Not only does this append the
current game log data to a running stats log file, but also logs the server
start and stop times.  If you want to shut it down, just resume the screen
and <Ctrl><C> once to kill the active process and then a second time during
the restart wait period.


Shockwave




More information about the Mohaa mailing list