Skip to content

Instantly share code, notes, and snippets.

@xfalcox
Created April 19, 2014 19:30
Show Gist options
  • Save xfalcox/11094907 to your computer and use it in GitHub Desktop.
Save xfalcox/11094907 to your computer and use it in GitHub Desktop.
#!/bin/sh
### BEGIN INIT INFO
# Provides: tf2server
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Team Fortress 2 server
# Description: Starts a Team Fortress 2 server
### END INIT INFO
NAME="Team Fortress 2"
USER="tf2server"
SCREENREF="tf2"
BINARYPATH="/home/tf2server/tf2_server"
BINARYNAME="srcds_run"
PIDFILE="tf2server.pid"
OPTS="-game tf +sv_pure 0 +maxplayers 32 +ip 0.0.0.0 +map pl_goldrush -autoupdate -steam_dir /home/tf2server/steamcmd/ -steamcmd_script /home/tf2server/steamcmd/tf2_ds.txt +sv_shutdown_timeout_minutes 5"
cd "$BINARYPATH"
running() {
if [ -n "`pgrep -f $BINARYNAME`" ]; then
return 0
else
return 1
fi
}
start() {
if ! running; then
echo -n "Starting the $NAME server... "
start-stop-daemon --start --chuid $USER --user $USER --chdir $BINARYPATH --exec "/usr/bin/screen" -- -dmS $SCREENREF $BINARYPATH/$BINARYNAME $OPTS
pgrep -f $BINARYNAME > $PIDFILE
if [ -s $PIDFILE ]; then
echo "Done"
else
echo "Failed"
rm $PIDFILE
fi
else
echo "The $NAME server is already started."
fi
}
stop() {
if running; then
echo -n "Stopping the $NAME server... "
kill `cat $PIDFILE`
while running; do
sleep 1
done
rm $PIDFILE
echo "Done"
else
echo "The $NAME server is already stopped."
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
if running; then
echo "The $NAME server is started."
else
echo "The $NAME server is stopped."
fi
;;
*)
echo "Usage: $0 (start|stop|restart|status)"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment