2

Here's a Challenge to all you boffins out there. Here's my issue:

I've got a Linux machine running 2 Scripts. 1 Script runs commands that affect the second script. The second script runs programs in a loop.

E.g. Script 1 - stop script 2 Script 1 - start script 2 This is in the form of service Script2 start / service Script2 stop. etc. *Note that service Script2 start creates a new detached screen with the second script.

The second script runs in a loop like:

  1. Run Update Script & Wait
  2. Run First Game & Wait
  3. Run Second Game & Wait

The games tend to run in their own screens, so when they are killed, the main script isn't. So if the game crashes, the Script2 doesn't crash.

Here's where the problem lies. I need to make it so that when I want to stop the server, it must first kill the game safely, e.g. allow time for it to shutdown. Then kill the Script2 screen and prevent the loop from starting the game again.

Any suggestions?

2
  • File that says running or not running... or maybe some other bit that tells scripts to either continue or exit. Just set exit bit when you are done. Don't have time to demonstrate now but hope you got it already. Commented Aug 22, 2012 at 17:47
  • @Sampo was thinking of doing the file thing, but seems a bit excessive and was looking for a simpler method. But thanks for the comment.
    – Skowt
    Commented Aug 22, 2012 at 17:50

1 Answer 1

1

You will need to put a conditional statement in your second script to check for some type of flag or signal before looping. If that flag or signal is present, your script should exit cleanly.

A simple such flag/signal is to check for a presence of a file, say /var/run/SCRIPTstop or similar. When you want to stop the script, a touch SCRIPTstop is all that is needed.

Depending on your scripting language you may be able to send it a UNIX signal, using kill: i.e. killall -SIGUSR1 $SCRIPTNAME, etc.

Your second script should ensure the file is deleted upon initialization, and probably would be nice for it to do that as well before exiting cleanly.

3
  • I've managed to get the game to exit safely in the second script by running this command: screen -S SCREENNAME -X stuff 'command'echo -ne '\003' - Which sends the CONTROL-C command to the game to exit safely. But the script loops again and starts the game again which causes issues.
    – Skowt
    Commented Aug 22, 2012 at 17:51
  • Unless you modify your script to check for something before looping (using a conditional, which is fancy talk for an if statement), it's going to keep doing what you originally told it to, which is to loop always.
    – LawrenceC
    Commented Aug 22, 2012 at 18:36
  • managed to create a file that was generated when the 'service Script2 stop' command was sent. Then added a check to the beginning of the loop which killed the script when the file was found but removed the file before killing the script. Works as it should. Thanks for the help and guidance! :D
    – Skowt
    Commented Aug 22, 2012 at 19:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .