Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

So I'm hosting a game that is very tempermental and can crash a lot on Linux, I made this cronjob to restart the screen and game should it fail:

#!/bin/bash
for t in {1..12} do
  if [ -z "$(ps -C game | grep game)" ]
  then
    /usr/bin/screen -d -m -S ss13 /usr/local/bin/game /home/gamefolder/game/game.dmb 1336
  fi
  sleep 5 
done

The cron job works just fine for starting the game server should the screen name not exist however if the game is running and crashes the cronjob fails to work and the game doesn't get restarted.

However the game is already running in the screen name ss13 and the screen itself doesn't crash, just the game. So I think what happens is that when the game crashes it tries to make a new screen with the game in but obviously can't. Is there any better method of doing this? Still quite a linux noob here.

share|improve this question

2 Answers 2

This is not really a job for cron, I would suggest something like the following running inside a screen session:

while :; do
  /usr/local/bin/game /home/gamefolder/game/game.dmb 1336
done

: is a shorthand for true.

The above assumes that the game doesn't daemonize. In that case use:

while :; do
  if [ -z "$(ps -C game | grep game)" ]; then
    /usr/local/bin/game /home/gamefolder/game/game.dmb 1336
  fi
  sleep 5
done
share|improve this answer
    
I wasn't aware you could do that in a screen session, my ineptness is showing through. I'll try it out anyway. –  Matt Jul 4 '12 at 23:14
    
screen is just a detachable terminal. Put the code above into a script and execute it within screen, e.g. /usr/bin/screen -d -m -S ss13 SCRIPT_NAME –  Thor Jul 4 '12 at 23:26

I'm familiar with using screen, but I'm not really sure what you're trying to do here. If you want the server to auto restart, then you don't really need screen for that. Perhaps you'd prefer a script that restarts immediately after a crash (instead of 5 seconds later).

#!/bin/bash
for t in {1..12}
do
    /usr/local/bin/game /home/gamefolder /game/game.dmb 1336
done
share|improve this answer
    
Sorry I'm not very good at explaining myself, I am trying to run the game in a screen however since the game crashes often I tried to make a cronjob which restarts the game in a new screen although it won't work as the screen it tries to create already exists from the game that was running. –  Matt Jul 4 '12 at 23:14
    
Try putting -X before the part you want to run in screen: -X "/usr/local/bin/game /home/gamefolder /game/game.dmb 1336" –  Sepero Jul 4 '12 at 23:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.