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.
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