CHOPPERGIRL'S AIRWAR

Home ~  Choppergirl ~  Dorothy ~  Alice ~  Wendy ~  14 Mile ~  Build Plan ~  Musicbox ~  Archive ~  Donate ~  Banners
 

CODE

CHOPPERGIRL'S LINUX GAME SERVER MONITOR AND RESTART SCRIPT

Game servers are notoriously unreliable; they die all the time. At least every couple of hours.

When they die, your game server is offline, at least until you realize this, and then you have to manual restart it.

This can happen many times a day, day in and day out every day of every year. You don't have time to monitor your server and restart it all the time.

There are products such as TCadmin, gamecp, SwiftPanel, god, Procautostart, ps-watcher, monit, and so on that purport to watch a (game server) process and start/stop/ restart it if it has crashed. Some have some other options to ftp up files with a web interface and edit some default files, and are all very complex pieces of software and cost money and add a lot of junk which you don't need. I got so fed up with trying to get any one of them to work that I finally wrote my own.

You don't need all that other crap. All you need to do all the work for you is a script which does one thing: watches a process and restarts it if it has died.

That Linux does not have a command built in to do this, is totally appalling to me.

To run a game server under Linux and mod it, all you need is the very basics:

  • a basic knowledge of working on a linux command line shell
  • an account on a linux box with bandwidth
  • httpd, ftpd, and telnetd server running on the linux box
  • an ftp client program to upload your game server directory to the linux box
  • your game server directory for the game you want to play
  • a text editor to edit text files
  • a way to unzip files
  • my script below
Once you have your game up and running under Linux, use my very simple code below that will watch your linux game server and restart it if it has died.



#!/bin/bash
# CHOPPERGIRLS LINUX GAME SERVER MONITOR AND RESTART SCRIPT
# check_and_restart_servers.sh
# http://air-war.org
# Monitor Linux Game Server Process with Cron Job and Restart Process If Not Present

# this is a (very simple) bash script, to do one thing, watch a process and restart it if it has died


# INSTALLATION INSTRUCTIONS

# STEP 1
# you may have to install bash as root with yum install bash
# but most systems have bash pre installed (do ls /bin/bash ) to check if unsure
# most likely it is, so proceed...

# In my examples, username where my server is installed is "airwar", replace with your own account

# Save this file in your home dir, in my case /home/airwar/check_and_restart_servers.sh
# make it executable with the command
# chmod a+x /home/airwar/check_and_restart_servers.sh

# STEP 2
# next, edit all the variables for your server below under 'config variables below'

# if you need to monitor more than one serer, copy the code under "FIRST SERVER"
# and paste it as many times for a SECOND SERVER, THIRD SERVER, etc.
# edit the variables to be correct for each one

# STEP 3
# to test if script works, run it as many times as you want from command line with
# ./check_and_restart_servers.sh
# if your server is not running, it will/should start it
# if its already running, it won't do anything but exit with a number (its own pid saying its finished)
# if the script dies on the first line, you probably added some garbage line breaks
# into the file with your text editor; if so, recreate the file using midnight commander editor
# $ yum install mc and recreate with "mcedit check_and_restart_servers.sh"

# STEP 4
# Once you know that it works, su root, and then edit /etc/rc.local and add these two lines
# so the script will be restarted with every reboot, in case entire system crashes... and it
# will bring your server right back up
# su airwar
# home/airwar/check_and_restart_servers.sh

# STEP 5
# if you ever need to bring down your server, first kill the check_and_restart_servers.sh
# proces in the process list (do ps -ax, look for its pid, then kill pidnumber
# then look for your game server pid the same way, and kill it as well.
# later I'll write a script to do this.

# STEP 6
# to make it survive reboots, edit /etc/rc.local and add this line,
# substituting username airwar and home directory to point to your script:
# sudo -u airwar nice /home/airwar/check_and_restart_servers.sh &

# --------------------------------------------------------------------
# MAIN INFINATE LOOP
while true
do

# --------------------------------------------------------------------

# FIRST SERVER

# config variables below
# path to pgrep command, mostly this is correct and you don't need to change it
PGREP="/usr/bin/pgrep"
# string of name of process you want to look for in process table
APPNAME="bf2_f"
# directory of where game dir process is to be restarted
RESTARTDIR="/home/airwar/bf2demov2"
# command to execute to restart process (add amperstand to background it and continue script)
RESTART="./start.sh &"


# here's the actual tiny program - now do actual check if server is listed in process list and if not, start it
# find APPNAMEs pid
$PGREP ${APPNAME}
if [ $? -ne 0 ] # if APPNAME not running
then
# restart APPNAME
echo Restarting Server
date
cd $RESTARTDIR
$RESTART
fi


# --------------------------------------------------------------------

# SECOND SERVER, etc - copy all code above for first server, paste here, and edit to suit for each server




# --------------------------------------------------------------------

# wait/sleep for x seconds, and then repeat whole loop
sleep 15
done

# --------------------------------------------------------------------


 


Add a Link to AIRWAR to your website, myspace page, or home page by adding the code below:

Home ~  Choppergirl ~  Dorothy ~  Alice ~  Wendy ~  14 Mile ~  Build Plan ~  Musicbox ~  Archive ~  Donate ~  Banners

Contact & Website Administrator: choppergirl@air-war.org