Q:

Cron Job that Boots Screen at Start

I am trying to set up a number of processes that start during boot (servers for games) with the below command as the cron item:

@reboot /usr/bin/screen -fa -d -m -S
  NAME COMMAND

However if the server crashes for what ever reason screen closes and the server doesn't get a chance to run it's auto restart (as far as I understand; screen sees no processes in the socket and so closes).
Is there a way that I can get around this so screen will sit there even if nothing is running in it? Like opening a shell?

A:

The command given is the child of screen, so when it exits, the window is dead.
You can get around this by starting a shell and then pushing input into the window to run the command, but this just changes the problem so that you'll be left with a lingering shell prompt and the server won't auto-restart.
Really, you want to run a keep-alive command, which then runs the server you're interested in.  There are many tools out there, but the simplest is to write something in shell yourself (unless you want the full power of the dedicated tools.
#!/bin/sh
while [ ! -f /home/servicename/shutdown-flagfile ]; do
  echo "Starting server [$(date)]"
  server-binary-name "$@"
  sleep 1
done

Line 2 lets you break out of the loop by creating a special file (with "touch"), the server-binary-name is the program to really run, "$@" is to pass through the options that the wrapper was invoked with, and sleep 1 is so that if the server is dying on start-up, you don't chew all the CPU repeatedly restarting it thousands of times per second, so that it's hard to get in and see what's happening.  The echo line just lets you see when the server last had to be restarted.

