#!/bin/bash
#
# Universal start/stop script
# - small modifications made for Ut2k4
# ! IF you want to use it for other games REMOVE these lines
# -> cd ${basedir}/System
# NOT THESE cd ${basedir}
#
# Script Version: 0.3
#
# ! awk needs to be installed !
#
# !! WARNING !!
# I am not responsible for damage caused by misconfigurations
# to your installation or any other damage that may occure.
#
# Info:
# This script is made by SkuLLy <info@dominating.nl>
#
# Support:
# sign up on the dominating.nl forums
# http://www.dominating.nl/forum
#
# Read the INSTALL file for more info about this script and ways
# to install it in crontab.
#
# Script is universal for different linux distro's
# this is done on purpose to make it easy to use
# and portable. We are NOT using start/stop daemon
# tools written for each distro e.g. start-stop-daemon
# on Debian.
#
# Vars
# basedir = game install directory without trailing slash!
# gamebin = name of the server executable
# gameopt = commandline options
# gamepid = process id file. Dont change it when not needed.
# gamelog = name and path of the logfile
#
# !! ONLY USE THIS WHEN YOU KNOW WHAT YOU ARE DOING !!
# s_enable = make use of screen when 1
# s_name = screen session name.
#
# Use different names for each session for easy use.
#
# Tested on:
# Debian
# CentOS 4.4
# Ubuntu
#
###################################################
#
#Config these setting according to your setup.
basedir="/digiex/ut2004"
gamename="Ut2k4"
gamebin="System/ucc-bin"
gamepid="${basedir}/ut2k4.pid"
gamelog="ut2k4.log"
gameopt="server VCTF-FaceClassic-SE-V2A?game=XGame.xVehicleCTFGame?mutator=XWeapons.MutNoSuperWeapon -multihome=93.127.227.61 ini=UT2004.ini log=UT2004.log -nohomedir &"
#
#Screen config
#
#ONLY USE SCREEN IF YOU KNOW HOW IT WORKS AND WANT IT.
#MAKE SURE SCREEN IS INSTALLED!!
s_enable=0
s_name="ut2k4_1"
##################################################
# NO CHANGES BELOW. ELSE ITS ON YOUR OWN RISK
#
#Checking the stuff
if [ ${s_enable} == 1 ] ; then
screen=`which screen`
fi
if [ -f "${basedir}/${gamebin}" ]; then
if [ ! -x "${basedir}/${gamebin}" ]; then
echo -e "${gamebin} file is not executable"
echo -e "Please fix this and try again"
exit 2
fi
else
echo "cannot find ${gamebin}!"
echo "If this is not correct edit the start script"
exit 2
fi
case "$1" in
start)
echo -n "Starting $gamename dedicated server: "
cd ${basedir}/System
if ps -ef |grep ${basedir}/${gamebin}|awk -F" " -v game=${basedir}/${gamebin} 'BEGIN {status=1} ; $8 == game {status=0} ; END {exit status}' ; then
echo -e "already active"
exit 3
else
if [ ${s_enable} == 1 ] ; then
cd ${basedir}
cp ${gamelog} ${gamelog}.crash
cd ${basedir}/System
${screen} -d -m -L -S ${s_name} $0 debug
else
cd ${basedir}
cp ${gamelog} ${gamelog}.crash
cd ${basedir}/System
if ${basedir}/${gamebin} ${gameopt} >> ${basedir}/${gamelog} & sleep 1 ; then
pid=`ps -ef |grep ${basedir}/${gamebin}|awk -F" " -v game=${basedir}/${gamebin} 'BEGIN {status=1} ; $8 == game { print $2 } ; END {exit status}'`
echo ${pid} > ${gamepid}
if [ -f "${gamepid}" ] && ps h `cat "${gamepid}"` >/dev/null; then
echo -e "....Started!"
exit 0
else
echo -e "....Failed to start. Check logfile or run in debug!"
exit 1
fi
else
echo -e "Failed"
fi
fi
fi
;;
stop)
echo -n "Stopping $gamename dedicated server: "
if ! ps -ef |grep ${basedir}/${gamebin}|awk -F" " -v game=${basedir}/${gamebin} 'BEGIN {status=1} ; $8 == game {status=0} ; END {exit status}' ; then
echo -e "server not running or crashed."
else
pid=`ps -ef |grep ${basedir}/${gamebin}|awk -F" " -v game=${basedir}/${gamebin} 'BEGIN {status=1} ; $8 == game { print $2 } ; END {exit status}'`
echo ${pid} > ${gamepid}
kill -9 `cat ${gamepid}`
if ! ps -ef |grep ${basedir}/${gamebin}|awk -F" " -v game=${basedir}/${gamebin} 'BEGIN {status=1} ; $8 == game {status=0} ; END {exit status}' ; then
echo -e "stopped"
exit 0
else
echo -e "unable to stop server or server crashed"
fi
fi
;;
status)
echo -n "`date +"%Y-%m-%d %H:%M:%S"` Checking $gamename dedicated server status: "
if ! ps -ef |grep ${basedir}/${gamebin}|awk -F" " -v game=${basedir}/${gamebin} 'BEGIN {status=1} ; $8 == game {status=0} ; END {exit status}' ; then
echo -e "server not running or crashed... Restarting"
if [ ${s_enable} == 1 ] ; then
cd ${basedir}
${screen} -d -m -L -S ${s_name} $0 debug
else
$0 start
fi
else
echo -e "Server still running."
fi
;;
check)
echo -n "Checking $gamename dedicated server status: "
if ! ps -ef |grep ${basedir}/${gamebin}|awk -F" " -v game=${basedir}/${gamebin} 'BEGIN {status=1} ; $8 == game {status=0} ; END {exit status}' ; then
echo -e "offline"
else
echo -e "online"
fi
;;
restart)
echo -e "Restarting $gamename dedicated server... "
$0 stop && $0 start
;;
debug)
echo -n "Starting debug mode for $gamename dedicated server: "
cd ${basedir}
if ps -ef |grep ${basedir}/${gamebin}|awk -F" " -v game=${basedir}/${gamebin} 'BEGIN {status=1} ; $8 == game {status=0} ; END {exit status}' ; then
echo -e "already active"
exit 3
else
echo "--DEBUG will appear on screen!"
echo "--Hit CTRL+C to kill the server!"
cd ${basedir}
cp ${gamelog} ${gamelog}.crash
cd ${basedir}/System
${basedir}/${gamebin} ${gameopt}
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status|check|debug}"
exit 1
esac