4

I'm running a game server on a remote server where I use a detached screen instance to leave it running. I'm now creating a script that can be used to shut down the server, back up all the vital files and start it up again, however I'm having a few difficulties with dealing with the screen.

I assumed that I could just switch into the detached screen in the script (after the server had already been shut down) by calling screen -r in the script. But that doesn't seem to work because if I run the script from outside screen it just launches the server in that session.

screen -r
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
screen -d

This is what I thought would do the trick but it doesn't. Maybe somebody can help me out here. I'm not a bash expert. In fact this is propably my first bash script that doesn't include "Hello World". Thanks.

2
  • 1
    Just take into account "Supervisor", the right tool for your needs Commented Jan 2, 2014 at 10:49
  • Might be a bit overkill for the ONE script that I want to run. But if my needs become more complex I'll be sure to check it out. Thanks. Commented Jan 2, 2014 at 10:58

1 Answer 1

5

Your script, as in your example, will get executed by your sell, not the one in the screen. You need to tell the running screen to read a file and execute it - that's what the -X option is for.

Try

tempfile=$(mktemp)
cat > $tempfile <<EOF
cd ~/servers/StarMade/
sh StarMade-dedicated-server-linux.sh
EOF
screen -X readbuf $tempfile
screen -X paste .
rm -f $tempfile

You can leave screen running in a 2nd terminal session to see what happens.

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, thanks for the quick reply. A few questions. First, what is that EOF thing you're doing there? And secondly screen -X attaches a command to a running screen session, right? Don't I need to specify which one? Lastly when I'm done with this, what do I need to change in my script? Or from where do I run it now?
The EOF thing is a here document, which means everything between the <<EOF and the EOF will be put into the standard input of cat, which writes it to the temp file. Second, -X will select the same screen that -r does - but you can combine -X with -r to select a specific one. Third, you replace your complete script with mine, and run it on the computer where screen is running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.