James Dawson
James Dawson

Reputation: 5409

Shell script to start game servers in screen

I'm trying to modify this very simple shell script I wrote to check if the screens are already active and if so, not create them. For example, if I call this script twice with the start parameter, four screen sessions will be made. I want to prevent that.

#! /bin/sh
# /etc/init.d/css-server
#

case "$1" in
  start)
    echo "Starting Nullus Imprimis war server..."
    screen -A -m -d -S css-war-server /home/css-servers/war-server/css/srcds_run -game cstrike +map de_dust2 +maxplayers 16 -autoupdate -port 2555
    echo "Nullus Imprimis war server started"
    echo "Starting Nullus Imprimis pub server #1..."
    screen -A -m -d -S css-pub-server-1 /home/css-servers/pub-server-1/css/srcds_run -game cstrike +map de_dust2 +maxplayers 32 -autoupdate -port 2666
    echo "Nullus Imprimis pub server #1 started"
    ;;
  stop)
    echo "Stopping Nullus Imprimis war server..."
    screen -S css-war-server -X quit
    echo "Nullus Imprimis war server stopped"
    echo "Stopping Nullus Imprimis pub server #1..."
    screen -S css-pub-server-1 -X quit
    echo "Nullus Imprimis pub server #1 stopped"
    ;;
  *)
    echo "Usage: service css-servers {start|stop}"
    exit 1
    ;;
esac

exit 0

Also, I want to make the servers run under their own username, in this case css-servers. How can I do that?

Upvotes: 0

Views: 606

Answers (1)

Thor
Thor

Reputation: 47189

To check if a screen is already running, I usually just grep it from screen -ls:

screen -ls | grep -q NAME || ...do something if server is not running...

Or:

if ! screen -ls | grep -q NAME; then
  ...do something if server is not running...
fi

In order to run this as a different user, I would suggest running the startup script with sudo -u, something like this:

sudo -u css-servers STARTUP_SCRIPT

Upvotes: 1

Related Questions