SRCDS Steam group


Linux init script to start server on boot
#16
Spartanfrog Wrote:Yeah your right lol. I know muppet was looking for beta testers for a control panel. Shoot him a PM.

Not me Toungue I already have one - the guy you're looking for is Masher.
Reply
#17
O crap yeah Im sorry. Too many M's Sad and its too late lol.
realchamp Wrote:
Hazz Wrote:Has someone helped you on these forums? If so, help someone else
Mooga Wrote:OrangeBox is a WHORE.
Reply
#18
Nice script, but i'm trying to modify it so it will run as a user(su - srcds) but without any luck Sad
Could you give me a hand here?

Code:
#!/bin/sh
# Source Dedicated Server Init Script

# Server options
TITLE='SRCDS Startup' # Script initialization title
LONGNAME='Team Fortress 2 Server 1'        # Full title of game type
NAME='srcds-tf2-1'                          # Server handle for the screen session
DAEMON='srcds_run'                # The server daemon
STEAM='/home/srcds/srcds_tf2/orangebox'        # STEAM to Steam installation
USER='srcds'

# Game options
IP='192.168.10.14'                # IP of the server
PORT='27015'                    # Port number to
MAP='ctf_2fort'                    # Initial map to start
GAME='tf'                        # Game type (tf|cstrike|valve|hl2mp)
SIZE='24'                        # Maximum number of players

# Server options string
OPTS="-game $GAME  +map $MAP +ip $IP -port $PORT \
    -autoupdate +maxplayers $SIZE -pidfile $STEAM/$GAME/$NAME.pid"

# Screen command
INTERFACE="/usr/bin/screen -A -m -d -S $NAME"

service_start() {
    # Check if the pid files currently exist
    if [ ! -f $STEAM/$GAME/$NAME.pid ] && [ ! -f $STEAM/$GAME/$NAME-screen.pid ]; then
        if [ -x $STEAM/$DAEMON ]; then
            echo "Starting $TITLE - $LONGNAME"
            echo "Server IP: $IP"
            echo "Server port: $PORT"
            echo "Server size: $SIZE players"
            sudo -u srcds /usr/bin/screen -A -d -m -S $NAME $STEAM/$DAEMON $OPTS
            # Prevent race condition on SMP kernels
             sleep 1
            # Find and write current process id of the screen process
            ps -ef | grep SCREEN | grep "$NAME" | grep -v grep | awk '{ print $2}' > $STEAM/$GAME/$NAME-screen.pid
            echo "$TITLE screen process ID written to $STEAM/$GAME/$NAME-screen.pid"
            echo "$TITLE server process ID written to $STEAM/$GAME/$NAME.pid"

            echo "$TITLE started."
        fi
    else
        echo -e "Cannot start $TITLE.  Server is already running."
        #exit 1
    fi
}

service_stop() {
    if [ -f $STEAM/$GAME/$NAME.pid ] && [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
        echo "Stopping $TITLE - $LONGNAME."
        # Get the process ID from the pid file we created earlier
        for id in `cat $STEAM/$GAME/$NAME-screen.pid`
            do kill -9 $id
            echo "Killing process ID $id"
            echo "Removing $TITLE screen pid file"
            rm -rf $STEAM/$GAME/$NAME-screen.pid
            break
        done
        # Remove server pid file
        echo "Removing $TITLE pid file"
        rm -rf $STEAM/$GAME/$NAME.pid
        # Wipe all old screen sessions
        screen -wipe 1> /dev/null 2> /dev/null
        echo "$TITLE stopped."
    else
        echo -e "Cannot stop $TITLE.  Server is not running."
        #exit 1
    fi
}


case "$1" in
    'start')
        service_start
        ;;
    'stop')
        service_stop
        ;;
    'restart')
        service_stop
        sleep 1
        service_start
        ;;
    *)
        echo "Usage $0 start|stop|restart"
esac
Reply
#19
Hey folks!

Long time to see, eh? Smile I put my SRCDS mini-projects on hiatus for a while, but now I'm back and hopefully I have helpful scripts to share!

First up is a TF2 Linux server install script. It's in no way what I would consider completed form. It will work as advertised (install a TF2 server) but there is just so much more I would like to add (server.cfg autocreation, interactive setup mode, etc.) Hope you enjoy and, as always, suggestions, comments and concerns are always welcome.

Cheers!
Dan
Code:
#!/usr/bin/env bash

###############################################################################
# TF2 server install script for Valve Software's SRCDS dedicated server
###############################################################################
# Author: Dan Ryan
# URL:    http://scriptfu.com/scripts/bash/srcds/installtf2.sh
# Copyright: 2008
# Version: 0.1
###############################################################################

# Sets up variables to be used in the rest of the script.
# TO DOS:
# Case statement checking
# Check if user exists
# Check if directory exists
# Check if srcds_run already exists, and if so, prompts for overwrite.

function steam_vars () {
    # Request the username
    echo "Please enter the desired username for the SRCDS server:"
    read STEAMUSER
    echo "Desired username is \"$STEAMUSER\". Please set the password:"
    # Request the password
    read STEAMPASS
    # Request the install directory
    echo "Please enter the desired install directory"
    read STEAMDIR
    echo "Default install directory is \"$STEAMDIR\". "
    sleep 2
}

# Create the user to run the server
# TODOS:
# Check if user currently exists

function create_user () {
    echo "Creating user to run the SRCDS server"
    # Flags:
    # -p Sets password
    # -d Sets home directory
    # -m Creates dir if nonexistant
    # -s Sets default shell, /sbin/nologin for no login (more secure)
    useradd -p $STEAMPASS -d $STEAMDIR -m -s /sbin/nologin $STEAMUSER
    wait $!
    echo "User \"$STEAMUSER\" created successfully!"
}

# Download and installs the HLDS tool.
# TODOS:
# Checks if file is already in $STEAMDIR, prompt for overwrite

function download_hlds () {
    # Switch to install directory
    cd $STEAMDIR
    # Download it,
    echo "Downloading HLDS Update Tool"
    wget http://storefront.steampowered.com/download/hldsupdatetool.bin
    wait $!
    echo "HLDS Update Tool successfully downloaded"; sleep 1
    # make it executable,
    echo "Making HLDS Update Tool executable"
    chmod +x hldsupdatetool.bin; wait $!;sleep 1
    # ...and run it.
    echo "Running HLDS Update Tool!"; sleep 2
    ./hldsupdatetool.bin; wait $!
    echo "HLDS Update Tool ran successfully."
}

#Update Steam game server
function update_steam () {
    #steam should be executable at this point
  #run steam once to update
  ./steam
  # run once more to begin downloading the files.  This will take a while.
  ./steam -command update -game "tf" -dir . ; wait $!
}

# Own all newly created files as the user
function chown_files () {
    # This is ugly and needs to be replaced with 'su - $STEAMUSER' farther up.
    chown $STEAMUSER:$STEAMUSER $STEAMDIR/*
}

# Run the functions!
steam_vars
create_user
download_hlds
update_steam
chown_files

# More to come in Ver. 0.2 !
# Interactive server.cfg setup
# Mods install (Mani, BeetlesMod, HLStatsX, SoureceMod/Metamod, and more)
# Chrooted environments
# Install init script based on distro (Debian/Red Hat)
=-=-=-=-=-=-=-=-=
[Image: banner_560x95.png]
Reply
#20
Thanks for such great scripts Scriptfu.

I went and edited your init script for functions I needed. I still gotta get some more stuff in there but this one will allow the server to keep running even if the whole machine was rebooted and pid remained.
Comand List: start|stop|restart|clear|update
start: will check the game folder for pid then verify if that pid is running on the server. If it is not it will then delete the pids and then start the server. If the server is running it will exit out like before.

stop: Acts the same only changed the AND to OR incase there is still a left over pid it will go through and clean up anything left.

restart: Behaves the same calling the stop and start commands like normal.

clear: Not really needed anymore but I left it in incase one wanted to clean up pid files after manually killing the server and screen.

update: will run the stop command and clear command. Then will update steam and run the game update. After the update finishes the server will be started.

The changes made will allow you to crontab a the start and update commands with out having to worry about orphen pids not allowing the server starting up. You can @reboot the start command and have it or have it run as often as you want to keep the server running incase of hard crash.

I'll be adding more changes and updates as the need arises for me.

Code:
#Source Dedicated Server Init Script

# Server options
TITLE='Source Dedicated Server' # Script initialization title
LONGNAME='Team Fortress 2'        # Full title of game type
NAME='tf2'                          # Server handle for the screen session
DAEMON='srcds_run'                # The server daemon
UPDATER='/usr/srcds'        # The Steam updater. I recommend keeping it one directory below orangebox for tf2 servers.
STEAM='/usr/srcds/orangebox'        # STEAM to Steam installation
USER='srcds'                        # User that this will be running under. Currently not functional part of this script.

# Game options
CLIENT='Team Fortress 2'    #Game Server name.
IP='127.0.0.1'                # IP of the server
PORT='27015'                    # Port number to
MAP='ctf_2fort'                    # Initial map to start
GAME='tf'                        # Game type (tf|cstrike|valve|hl2mp)
SIZE='32'                        # Maximum number of players
HIGHPRIORITY=1                #Set server renice to -20 will make server take priority over all other applications on server. 1 being on and 0 being off.

# Server options string
OPTS="-game $GAME +hostname \"$CLIENT\" +map $MAP +ip $IP -port $PORT \
    -autoupdate +maxplayers $SIZE -pidfile $STEAM/$GAME/$NAME.pid"

# Screen command
INTERFACE="/usr/bin/screen -A -m -d -S $NAME"

service_start() {
    # Check if the pid files currently exist
    if [ -f $STEAM/$GAME/$NAME.pid ] || [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
        # Pid files allready exist check if the process is still running.
        if [ "$(ps -p `cat $STEAM/$GAME/$NAME.pid` | wc -l)" -gt 1 ]; then
            # Process is still running.
            echo -e "Cannot start $TITLE.  Server is already running."
        #exit 1
        else
        # Process exited with out cleaning up pid files.
            if [ "$(ps -p `cat $STEAM/$GAME/$NAME.pid` | wc -l)" -gt 1 ]; then
            # Screen is still running.
            # Get the process ID from the pid file we created earlier
                for id in `cat $STEAM/$GAME/$NAME-screen.pid`
                do kill -9 $id
                echo "Killing process ID $id"
                echo "Removing $TITLE screen pid file"
                rm -rf $STEAM/$GAME/$NAME-screen.pid
                break
                done
            fi
        # Remove server pid file
        echo "Removing $TITLE pid file"
        rm -rf $STEAM/$GAME/$NAME.pid
        # Wipe all old screen sessions
        screen -wipe 1> /dev/null 2> /dev/null
        service_start
        fi
    else
    # Server is not running start the server.
        if [ -x $STEAM/$DAEMON ]; then
            echo "Starting $TITLE - $LONGNAME - $CLIENT"
            echo "Server IP: $IP"
            echo "Server port: $PORT"
            echo "Server size: $SIZE players"
            cd $STEAM
            $INTERFACE $STEAM/$DAEMON $OPTS
            # Prevent race condition on SMP kernels
             sleep 1
            # Find and write current process id of the screen process
            ps -ef | grep SCREEN | grep "$NAME" | grep -v grep | awk '{ print $2}' > $STEAM/$GAME/$NAME-screen.pid
            echo "$TITLE screen process ID written to $STEAM/$GAME/$NAME-screen.pid"
            echo "$TITLE server process ID written to $STEAM/$GAME/$NAME.pid"
            
            echo "$TITLE started."
            # Was having problems with directory permisions due to ftp access making these files unreadable by users other than owner.
            chmod 666 $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null
            sleep 2
            if [ $HIGHPRIORITY = 1 ]; then
                renice -20 `cat $STEAM/$GAME/$NAME.pid` >/dev/null 2>&1
            fi
        fi
    fi
}

service_stop() {
    if [ -f $STEAM/$GAME/$NAME.pid ] || [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
        echo "Stopping $TITLE - $LONGNAME."
        # Get the process ID from the pid file we created earlier
        for id in `cat $STEAM/$GAME/$NAME-screen.pid`
            do kill -9 $id
            echo "Killing process ID $id"
            echo "Removing $TITLE screen pid file"
            rm -rf $STEAM/$GAME/$NAME-screen.pid
            break
        done
        # Remove server pid file
        echo "Removing $TITLE pid file"
        rm -rf $STEAM/$GAME/$NAME.pid
        # Wipe all old screen sessions
        screen -wipe 1> /dev/null 2> /dev/null
        echo "$TITLE stopped."
    else
        echo -e "Cannot stop $TITLE.  Server is not running."
        #exit 1
    fi    
}    
service_clear() {
    # Removing all pid files
    echo "Removing all Service pid files."
    rm -rf $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null
}

service_update() {
    echo "Stopping and Clearing all Service files."
    service_stop
    sleep 2
    service_clear
    sleep 2
    echo "Updating Steam Updater"
    cd $UPDATER
    ./steam 1> /dev/null 2> /dev/null
    echo "Updating Game Files"
    ./steam -command update -game $GAME -dir . 1> /dev/null 2> /dev/null
    sleep 2
    service_start
}


case "$1" in
    'start')
        service_start
        ;;
    'stop')
        service_stop
        ;;
    'restart')
        service_stop
        sleep 1
        service_start
        ;;
    'clear')
        service_clear
        ;;
    'update')
        service_update
        ;;
    *)
        echo "Usage $0 start|stop|restart|clear|update"
esac
Reply
#21
scriptfu Wrote:Hi everyone,

It's always a good idea to run any service as a user instead of root, so I'll be modifying this script to run as a user. Would it be beneficial to anyone if I wrote a tutorial on setting up a chrooted SRCDS environment? I have seen many tutorials on setting up SRCDS, but none yet on setting up SRCDS in a secure manner.

Cheers,
Dan

A small change in the above script could be this:

Code:
USER_NAME="tf2"
CURRENT_USER=$(/usr/bin/whoami)

and for starting the app:
Code:
if [ "$CURRENT_USER" = "$USER_NAME" ]; then
    sh -c "$INTERFACE $STEAM/$DAEMON $OPTS"
  else
    su - $USER_NAME -c "$INTERFACE $STEAM/$DAEMON $OPTS"
  fi

A chrooted environment setup would be interesting to see.
Reply
#22
ogre.

su would work since we just be using it for the server startup command. It just needs to be run by root or the actually user. I forget these things but I think any user that doesn't have the right to run as the intended user will prompt for password. Would be fine if its intended during that case to be used by an actual user. It would render the script useless in cronjobs.

Wasn't going to test this but it just peaked my interest and now I am not sleeping. Rolleyes oh well. I did get it to work. There should be no need for all the if statements since sudo will run with whatever user you set it to even if its the same user running it but I think you need to do a little bit more on the system side by adding the user into sudoers so for the sake of saving additional headaches I will just use the if statements. Also this is just changing for the server start up I didn't add it in for updating. but if you just follow the same idea below it would work.

If you want to to this just replace the line
Code:
# Screen command
INTERFACE="/usr/bin/screen -A -m -d -S $NAME"

with

Code:
# Screen command
CURRENT_USER=$(/usr/bin/whoami)
if [ "$CURRENT_USER" = "$USER" ]; then
    INTERFACE="/usr/bin/screen -A -m -d -S $NAME"
  else
    INTERFACE="sudo -u $USER /usr/bin/screen -A -m -d -S $NAME"
fi

and after
Code:
chmod 666 $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null

add

Code:
# Make any pid files created by different users owned by the set user.
            chown $USER $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null

Updated with working changes.

Code:
Code:
#Source Dedicated Server Init Script

# Server options
TITLE='Source Dedicated Server' # Script initialization title
LONGNAME='Team Fortress 2'        # Full title of game type
NAME='tf2'                          # Server handle for the screen session
DAEMON='srcds_run'                # The server daemon
UPDATER='/usr/srcds'        # The Steam updater. I recommend keeping it one directory below orangebox for tf2 servers.
STEAM='/usr/srcds/orangebox'        # STEAM to Steam installation
USER='srcds'                        # User that this will be running under. Currently not functional part of this script.

# Game options
CLIENT='Team Fortress 2'    #Game Server name.
IP='127.0.0.1'                # IP of the server
PORT='27015'                    # Port number to
MAP='ctf_2fort'                    # Initial map to start
GAME='tf'                        # Game type (tf|cstrike|valve|hl2mp)
SIZE='32'                        # Maximum number of players
HIGHPRIORITY=1                #Set server renice to -20 will make server take priority over all other applications on server. 1 being on and 0 being off.

# Server options string
OPTS="-game $GAME +hostname \"$CLIENT\" +map $MAP +ip $IP -port $PORT \
    -autoupdate +maxplayers $SIZE -pidfile $STEAM/$GAME/$NAME.pid"

# Screen command
CURRENT_USER=$(/usr/bin/whoami)
if [ "$CURRENT_USER" = "$USER" ]; then
    INTERFACE="/usr/bin/screen -A -m -d -S $NAME"
  else
    INTERFACE="sudo -u $USER /usr/bin/screen -A -m -d -S $NAME"
fi


service_start() {
    # Check if the pid files currently exist
    if [ -f $STEAM/$GAME/$NAME.pid ] || [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
        # Pid files allready exist check if the process is still running.
        if [ "$(ps -p `cat $STEAM/$GAME/$NAME.pid` | wc -l)" -gt 1 ]; then
            # Process is still running.
            echo -e "Cannot start $TITLE.  Server is already running."
        #exit 1
        else
        # Process exited with out cleaning up pid files.
            if [ "$(ps -p `cat $STEAM/$GAME/$NAME.pid` | wc -l)" -gt 1 ]; then
            # Screen is still running.
            # Get the process ID from the pid file we created earlier
                for id in `cat $STEAM/$GAME/$NAME-screen.pid`
                do kill -9 $id
                echo "Killing process ID $id"
                echo "Removing $TITLE screen pid file"
                rm -rf $STEAM/$GAME/$NAME-screen.pid
                break
                done
            fi
        # Remove server pid file
        echo "Removing $TITLE pid file"
        rm -rf $STEAM/$GAME/$NAME.pid
        # Wipe all old screen sessions
        screen -wipe 1> /dev/null 2> /dev/null
        service_start
        fi
    else
    # Server is not running start the server.
        if [ -x $STEAM/$DAEMON ]; then
            echo "Starting $TITLE - $LONGNAME - $CLIENT"
            echo "Server IP: $IP"
            echo "Server port: $PORT"
            echo "Server size: $SIZE players"
            cd $STEAM
            $INTERFACE $STEAM/$DAEMON $OPTS
            # Prevent race condition on SMP kernels
             sleep 1
            # Find and write current process id of the screen process
            ps -ef | grep SCREEN | grep "$NAME" | grep -v grep | awk '{ print $2}' > $STEAM/$GAME/$NAME-screen.pid
            echo "$TITLE screen process ID written to $STEAM/$GAME/$NAME-screen.pid"
            echo "$TITLE server process ID written to $STEAM/$GAME/$NAME.pid"
            
            echo "$TITLE started."
            # Was having problems with directory permisions due to ftp access making these files unreadable by users other than owner.
            chmod 666 $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null
            # Make any pid files created by different users owned by the set user.
            chown $USER $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null
            sleep 2
            if [ $HIGHPRIORITY = 1 ]; then
                renice -20 `cat $STEAM/$GAME/$NAME.pid` >/dev/null 2>&1
            fi
        fi
    fi
}

service_stop() {
    if [ -f $STEAM/$GAME/$NAME.pid ] || [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
        echo "Stopping $TITLE - $LONGNAME."
        # Get the process ID from the pid file we created earlier
        for id in `cat $STEAM/$GAME/$NAME-screen.pid`
            do kill -9 $id
            echo "Killing process ID $id"
            echo "Removing $TITLE screen pid file"
            rm -rf $STEAM/$GAME/$NAME-screen.pid
            break
        done
        # Remove server pid file
        echo "Removing $TITLE pid file"
        rm -rf $STEAM/$GAME/$NAME.pid
        # Wipe all old screen sessions
        screen -wipe 1> /dev/null 2> /dev/null
        echo "$TITLE stopped."
    else
        echo -e "Cannot stop $TITLE.  Server is not running."
        #exit 1
    fi    
}    
service_clear() {
    # Removing all pid files
    echo "Removing all Service pid files."
    rm -rf $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null
}

service_update() {
    echo "Stopping and Clearing all Service files."
    service_stop
    sleep 2
    service_clear
    sleep 2
    echo "Updating Steam Updater"
    cd $UPDATER
    ./steam 1> /dev/null 2> /dev/null
    echo "Updating Game Files"
    ./steam -command update -game $GAME -dir . 1> /dev/null 2> /dev/null
    sleep 2
    service_start
}


case "$1" in
    'start')
        service_start
        ;;
    'stop')
        service_stop
        ;;
    'restart')
        service_stop
        sleep 1
        service_start
        ;;
    'clear')
        service_clear
        ;;
    'update')
        service_update
        ;;
    *)
        echo "Usage $0 start|stop|restart|clear|update"
esac
Reply
#23
Arujei Wrote:ogre.

su would work since we just be using it for the server startup command. It just needs to be run by root or the actually user. I forget these things but I think any user that doesn't have the right to run as the intended user will prompt for password. Would be fine if its intended during that case to be used by an actual user. It would render the script useless in cronjobs.

I run my cronjobs related to that user as that user, so cron jobs work fine. As you say, when i use su I don't have to add sudoers. Since only root and the actual user should be able to run the file it works fine for my purposes.
Reply
#24
Thanks Arujei, if users want to know how to protect this as well, here it is, copy everything Arujei updated:

Since it isn't a great idea to run this script as root, specifying which user to update it would be a better and ideal way to do this. To run this script in a chrooted environment, one can do the following:

So we have to keep in mind that a server might have multiple users, so I said user "tf2dev" owns the server files.

So in the sudoers file
One thing to change is to add an item to the sudoers file
Code:
sudo visudo /etc/sudoers

Add this to the end:
Code:
%tf2dev  ALL=(tf2dev) NOPASSWD: /usr/local/games/tf2dev/

That says, any person in group "tf2dev" can run any script inside "/usr/local/games/tf2dev/" with the user "tf2dev" of course with no password.

That will restrict only users in tf2dev group to execute anything in that directory.

Then you will create a tunnel script so that you can run that file with sudo tf2dev
Code:
#! /bin/sh
sudo -u tf2dev /usr/local/games/tf2dev/server-launch $@

Server-launch is the original script.

Now inside the current (original) script, only one thing needs to be modified... would be this area:

Find this:
Code:
# Screen command
CURRENT_USER=$(/usr/bin/whoami)
if [ "$CURRENT_USER" = "$USER" ]; then
    INTERFACE="/usr/bin/screen -A -m -d -S $NAME"
  else
    INTERFACE="sudo -u $USER /usr/bin/screen -A -m -d -S $NAME"
fi

Replace with this:
Code:
INTERFACE="/usr/bin/screen -A -m -d -S $NAME"

# Screen command
CURRENT_USER=$(/usr/bin/whoami)

if [ "$CURRENT_USER" != "$USER" ]; then
    echo "$TITLE cannot run on user ($CURRENT_USER)";
    exit
fi

That will force the user to run under the script under $USER. So now anyone with tf2dev as their group could start, run, and execute it.

Now the question would be, how would we allow different users to screen into that server?
Reply
#25
nevermind, got it now
Reply
#26
Having a problem with this script. It's throwing errors on 'service_stop'.

The error is:

Code:
Stopping server
./server_run.sh: line 81: kill: cat: arguments must be process or job IDs
./server_run.sh: line 81: kill: /home/css/cstrike/server-screen.pid: arguments must be process or job IDs
Killing process ID cat /home/css/cstrike/server-screen.pid
Removing screen PID file
Removing server PID file
Done.

The script:

Code:
service_start() {
    echo "Starting $TITLE"
    if [ -f $STEAM/$GAME/$NAME.pid ] || [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
      if [ "$(ps -p 'cat $STEAM/$GAME/$NAME.pid' | wc -l)" gt -1 ]; then
        echo -e "Error: Server is already online."
      else
        if [ "$(ps -p 'cat $STEAM/$GAME/$NAME.pid' | wc -l)" -gt 1 ]; then
          for id in 'cat $STEAM/$GAME/$NAME.pid'
            do kill -9 $id
            echo "Killing process $id."
            echo "Removing screen PID file."
            rm -rf $STEAM/$GAME/$NAME-screen.pid
            break
          done
        fi
        echo "Removing server PID file."
        rm -rf $STEAM/$GAME/$NAME.pid
        screen -wipe 1> /dev/null 2> /dev/null
        service_start
      fi
    else
      if [ -x $STEAM/$DAEMON ]; then
        echo "----------------------------------------"
        echo "Name:        $TITLE"
        echo "Game:        $SRCDS"
        echo "IP:        $IP"
        echo "Port:        $PORT"
        echo "Slots:    $SLOTS players"
        echo "----------------------------------------"
        cd $STEAM
        $INTERFACE $STEAM/$DAEMON $OPTS
        sleep 1
        ps -ef | grep SCREEN | grep "$NAME" | grep -v grep | awk '{print $2}' > $STEAM/$GAME/$NAME-screen.pid
        echo "$TITLE screen process:"
        echo "  $STEAM/$GAME/$NAME-screen.pid"
        echo "$TITLE server process:"
        echo "  $STEAM/$GAME/$NAME.pid"
        chmod 666 $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null
        chown $USER $STEAM/$GAME/*.pid 1> /dev/null 2> /dev/null
        sleep 2
        if [ $HIGHPRIORITY = 1 ]; then
          renice -20 'cat $STEAM/$GAME/$NAME.pid' > /dev/null 2>&1
        fi
        echo "----------------------------------------"
        echo 'Done.'
      else
        echo 'Error: Server could not be started.'
      fi
    fi
}

service_stop() {
    echo "Stopping $TITLE"
    if [ -f $STEAM/$GAME/$NAME.pid ] || [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
        for id in 'cat $STEAM/$GAME/$NAME-screen.pid'
            do kill -9 $id
            echo "Killing process ID $id"
            echo 'Removing screen PID file'
            rm -rf $STEAM/$GAME/$NAME-screen.pid
            break
        done
        echo 'Removing server PID file'
        rm -rf $STEAM/$GAME/$NAME.pid
        screen -wipe 1> /dev/null 2> /dev/null
        echo 'Done.'
    else
        echo 'Error: Server not online.'
    fi
}
Reply
#27
scriptfu Wrote:Hmm, I only host one server for now, and it's not even populated 90% of the time Toungue I'm surprised there's no open source one out there. Heck, I think I just found my next project Smile

Cheers,
Dan

there is one: hldstart
Reply
#28
Code:
# Game options
IP='72.52.248.250'                # IP of the server
PORT='27015'                    # Port number to

Possible to use a dyndns adress instead of an ip number. My ip changes now and then. Not so often that it disturbs but it would be easier if could use a dyndns adress. Or even better if if the script takes the info from eth0
Reply
#29
Arujei thank you very much for your script modification! Everything works GREAT!
But i`ve found one bug. Which cases baaad errors...

Here is the fix:
Code:
service_start() {

    # Check if the pid files currently exist
    if [ -f $STEAM/$GAME/$NAME.pid ] || [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
        # Pid files allready exist check if the process is still running.

        if [ "$(ps -p `cat $STEAM/$GAME/$NAME.pid` | wc -l)" -gt 1 ]; then
            # Process is still running.
            echo -e "Cannot start $TITLE.  Server is already running."
        #exit 1
        else
        # Process exited with out cleaning up pid files.
            if [ "$(ps -p `cat $STEAM/$GAME/$NAME.pid` | wc -l)" -gt 1 ]; then
            # Screen is still running.
            # Get the process ID from the pid file we created earlier
                kill -9 `cat $STEAM/$GAME/$NAME-screen.pid`
                echo "Killing process ID $id"
                echo "Removing $TITLE screen pid file"
                rm -rf $STEAM/$GAME/$NAME-screen.pid
break
            fi
if [ -f $STEAM/$GAME/$NAME-screen.pid ]; then
rm -rf $STEAM/$GAME/$NAME-screen.pid
fi

        # Remove server pid file
        echo "Removing $TITLE pid file"
        rm -rf $STEAM/$GAME/$NAME.pid
        # Wipe all old screen sessions
        screen -wipe 1> /dev/null 2> /dev/null
        service_start
        fi
    else
    # Server is not running start the server.
        if [ -x $STEAM/$DAEMON ]; then
            echo "Starting $TITLE - $LONGNAME - $CLIENT"
            echo "Server IP: $IP"
            echo "Server port: $PORT"
            echo "Server size: $SIZE players"
            cd $STEAM
            $INTERFACE $STEAM/$DAEMON $OPTS
            # Prevent race condition on SMP kernels
             sleep 1
            # Find and write current process id of the screen process
            ps -ef | grep SCREEN | grep "$NAME" | grep -v grep | awk '{ print $2}' > $STEAM/$GAME/$NAME-screen.pid
            echo "$TITLE screen process ID written to $STEAM/$GAME/$NAME-screen.pid"
            echo "$TITLE server process ID written to $STEAM/$GAME/$NAME.pid"
            
            echo "$TITLE started."
            # Was having problems with directory permisions due to ftp access making these files unreadable by users other than owner.
            chmod 666 $STEAM/$GAME/*.pid #1> /dev/null 2> /dev/null
            # Make any pid files created by different users owned by the set user.
            chown $USER $STEAM/$GAME/*.pid #1> /dev/null 2> /dev/null
            sleep 2
            if [ $HIGHPRIORITY = 1 ]; then
                renice -20 `cat $STEAM/$GAME/$NAME.pid` >/dev/null 2>&1
            fi
        fi
    fi
}

if you shutdown your pc inproperly the pid file is not deleted. I`ve fixed it. Tested on Gentoo Linux.
[Image: b_560x95.png]
Reply
#30
Sorry for digging up an old thread, but I'm wondering what you all would recommend for when you want to run multiple servers.

Right now I'm modifying the code to start CSS with the startup script, but I'm also making it expandable for L4D, and L4D2.

Right now I'm writing the script in Perl, and possibly python, but I think this is getting over complicated, anyone recommend something different?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)