A present from the 3rd ID - A fully functional start | stop | restart bash script

Joe Hunkeler wolve at dodfrontline.com
Thu Jan 27 14:39:45 EST 2005


One of my system administrators wrote this script... 
See, this is what happens when he gets pissed off with Call of Duty (or 
Quake III in general).

The script requires 'qstat' which you can obtain from:  http://www.qstat.org

ENJOY :)

--- Stop copy below this line ---

#!/bin/bash
# Call of Duty: United Offensive - Server Manager 1.0.1
# Copyright (C) 2005 Alan LeVee <alan.levee at prometheus-designs.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILIITY of FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have recieved a copy of the GNU General Public LIcense
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 300, Boston, MA 02111-1307, USA.
#
# Special Thanks: To andabata- of the ChatJunkies IRC Network for helping me
#                 figure out the major bugs in the process id managing and
#                 creation process.

#
# ---------------------- Configurable Server Options -----------------------
#
# codpath - This variable should be set with the full qualified path to your
#           Call of Duty / Call of Duty: United Offensive installation. 
Please
#           no trailing slashes.
#
codpath="/path/to/callofduty"

#
# servercfg - This variable should be set with the exact name of the server
#             configuration file that is to be used in running the server.
#
srvrcfg="dedicated.cfg"

#
# inet_listen_ip - This variable should be set to the IP address (if you 
have multiple
#                  interfaces) the server should listen on. If you only 
have one interface
#                  or you desire to have the Call of Duty server listen 
on ALL interfaces
#                  do not enter anything.
#
#                  Example:
#                  inet_listen_ip="69.93.51.186" - for a server with 
multiple IP addresses.
#                  inet_listen_ip=""             - if you desire to 
listen on all interfaces,
#                                                  or only have one IP 
address.
#
inet_listen_ip=""

#
# inet_listen_port - This variable should be set to the UDP port you 
wish to have the server
#                    listen on. If you are hosting multiple servers on 
one IP address this is
#                    the only reason to modify it, otherwise please 
leave it to the default
#                    Call of Duty listening port.
#
inet_listen_port="28960"

#
# srvr_fspath - This variable should be set to the fully qualified 
fs_path for the server.
#               Generally you only need to set this variable if you have 
custom game server
#               directory seperations, otherwise just leave it blank and 
the server will
#               configure this variable itself. However you can force 
the default anyway.
#
#               Example:
#
#               Let's say you have Call of Duty installed in 
/home/callofduty. Your srvr_fspath
#               variable would be like this:
#
#               srvr_fspath="/home/callofduty/.callofduty"
#
srvr_fspath=""

#
# qstat_binary - This variable should be set to the full $PATH call to 
qstat. Qstat is required
#                by this script to report status of the server. By 
default not all distributions
#                have qstat installed so you'll need to go to 
http://www.qstat.org and install
#                it and then set the path.
#
#                NOTE: For Debian GNU/Linux users, your qstat 
installation is /usr/bin/quakestat
#
qstat_binary="/usr/bin/qstat"

# ---------------------- End - DO NOT CHANGE ANYTHING BELOW! 
-----------------------

#
# start()
#
# This function will create the server process and write it to a pID file.
#
function start()
{
    # Check the user.
    check_user
    
    # Check the pID file status, make sure the server is not already running
    # or that the pID file is not stale.
    if [ -r "${pidfile}" ]; then
        running=""
        /bin/ps -p `cat ${pidfile}` &>/dev/null && running="yes"

        if [ -n "$running" ]; then
            echo "Call of Duty server is already running as pid: `cat 
${pidfile}`."
            exit
        else
            echo "Found pID file but Call of Duty is not running, 
deleting the stale pID file."
            rm -rf ${pidfile}
        fi
    fi
    
    # Attempt to start Call of Duty server.
    ${codpath}/coduo_lnxded \
    +set fs_basepath ${srvr_fspath} \
    +set sv_pure 1 \
    +set dedicated 2 \
    +set ttycon 0 \
    +set net_ip ${inet_listen_ip} \
    +set net_port ${inet_listen_port} \
    +set fs_homepath ${codpath} \
    +exec ${srvrcfg} &>/dev/null &

    # Report that the server has been started and show the pID
    # it has.
    echo "Call of Duty has been started and the assigned pID is: $!"
    echo $! >${pidfile}
}

#
# check_pidfile()
#
# This function will check to make sure that the pID file is present.
#
function check_pidfile()
{
    if [ -z "${pidfile}" ]; then
        # The pID file is missing, abort the process.
        echo "Aborting... No Call of Duty server is running, or the pID 
file is missing."
        exit
    else
        # The pID file has been found.
        true
    fi
}

#
# stop()
#
# This function will stop the server as the user has requested.
#
function stop()
{
    # Check for the user root.
    check_user
    
    # Check for a pID file.
    check_pidfile

    # If the pID file does exist, then kill the server.
    echo "Stopping the Call of Duty server..."
    kill -9 "`cat ${pidfile}`"
    rm -rf ${pidfile}
}

#
# usage()
#
# This function will display usage of the script when no arguments
# are proceeded by the command.
#
function usage()
{
    echo "usage $0: [options]"
    echo "start         - starts the call of duty server in the background."
    echo "stop          - stops the call of duty server."
    echo "restart       - restarts the call of duty server on the same 
map it was running."
    echo "status        - shows the current status of the server using 
qStat."
    echo "version       - display the current version of the script."
}

#
# version
#
# This function will display the version of the script.
#
function version()
{
    echo "Call of Duty: United Offensive - Server Manager 1.0.1"
    echo "By Alan LeVee <alan.levee at prometheus-designs.net>"
}

#
# check_qstat()
#
# This function will check to see if you have a qStat installation. If 
you do not
# it will then report that it is not found and what to do to fix it.
#
function check_qstat()
{
    if [ -x "${qstat_binary}" ]; then
        # Found qStat
        true
    else
        echo "Aborting... qStat was not found (${qstat_binary})."
        echo "Please double check your setting for qstat_binary and"
        echo "make sure you have qStat installed. Otherwise please go"
        echo "to http://www.qstat.org and download it."
        exit
    fi
}

#
# restart()
#
# This function will do a complete restart of the Call of Duty server and
# also load it on the last map it was on before the restart. Also this
# function _will_ not restart the server if players are in the server.
#
function restart()
{
    # Check for the user root.
    check_user
    
    # Check for qStat and the pID file.
    check_qstat
    check_pidfile
    
    # Show a status report of the server.
    echo "Querying - ${inet_listen_ip}:${inet_listen_port} with 
${qstat_binary}"
    rawqstat="`${qstat_binary} -nh -q3s 
${inet_listen_ip}:${inet_listen_port}`"
    players="`echo ${rawqstat} | awk '{ print $2 }' | cut -f1 -d "/"`"
    map="`echo ${rawqstat} | sed -e 's/\/ //' | awk '{print $3 }'`"

    echo "Server currently has ${players} on the map: ${map}"

    if [ "${players}" == "0" -o "${players}" == "ERROR" -o "${players}" 
== "DOWN" ]; then
        echo "Attempting to restart the server..."
        stop
        start
    else
        echo "The server is not empty! Not attempting restart."
    fi
}

#
# check_user()
#
# This function will make sure to check that the user is not root to 
prevent
# the server from being used as a catalyst for hacking the server.
function check_user()
{
    if [ $UID == 0 ]; then
        echo "You cannot run the Call of Duty server as root user. "
        echo "Please either login to the appropriate user for the"
        echo "Call of Duty server using 'su' or 'sudo'. If you have"
        echo "created a user/group for the server. Please review the"
        echo "manpages for useradd and groupadd and then run this script"
        echo "again."
        echo
        echo "For more information please review the follow man pages:"
        echo "man sudo"
        echo "man su"
        echo "man useradd"
        echo "man groupadd"
        exit
    else
        true
    fi
}

#
# status()
#
# This function will report back a status of the server using qStat.
#
function status()
{
    ${qstat_binary} -P -q3s ${inet_listen_ip}:${inet_listen_port}
}

# pID file writing paths.
pidfile_path="${codpath}"
pidfile="${pidfile_path}/cod-${USER}-${inet_listen_ip}.${inet_listen_port}.pid"

if [ -z "${inet_listen_ip}" ]; then
    # Assume localhost if empty
    inet_listen_ip="localhost"
fi

# Parse the available options.
case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        status
        ;;
    version)
        version
        ;;
    *)
        usage
esac

--- Stop copy above this line ---



More information about the Cod mailing list