Auto-restart “screen” script not working

automationgnu-screenscriptingservice

I'm looking over a script that would actually be restarting game-servers in case they crash. The game-servers are actually started in a screen session through a control panel. They have to be restarted by killing their respective screen session and starting them again in a new screen session with the same name. I've done this script by my self but it doesn't really work and runs into errors.

#!/bin/bash

START='cd /home/test; ./GameServer +set parameters'
SCREEN_NAME="test"


while /bin/true; do


sleep 5
SERVER=`ps --User test | grep GameServer | grep -v grep | wc -l`

 if [[ $SERVER -eq "0" ]]; then
    screen -S $SCREEN_NAME-X quit ; screen -S $SCREEN_NAME -md $START
    sleep 10
 fi
done

The process name is "gameserver" and an issue is with killing the screen session because it won't quit and if I kill all screen sessions, the screen running the script also quits.

test@de:~$ screen -list
There is a screen on:
        31861.2-test    (08/11/13 12:51:06)     (Detached)
1 Socket in /var/run/screen/S-test.

test@de:~$ screen -r 2-test -X quit
No screen session found.
test@de:~$ screen -x 2-test -X quit
No screen session found.
test@de:~$ screen -S 2-test -X quit
No screen session found.

I can although attach to the above screen by "screen -x" and I can also kill a screen with the same name using the above commands if I start it as: "screen -S test". I believe there's something in the Control panel that starts it in a different way such that it fails to attach using the name.

Best Answer

Aw come on, don't use screen for this.

Use something that's actually designed to look after processes, and restart/respawn them when they die.

Anything else is just wheel-reinvention, and will never be as good or as tested.