0

I've been trying to write a script that creates a screen session and starts a game server, which I actually managed to do. But the problem is reattaching to that session after detaching... I know the server is running after detaching from the session, but when writing screen -ls my session does not appear. Even if I type Cntrl+A " there's no session displayed. Trying screen -r tells me there's no session but I'm 100% sure there is since the server is still up and typing ps au shows me the process. What am I doing wrong?

Also, creating a session and running the server in it, detaching and then re-attaching works. It's just when done on a script that it doesn't reattach nor find it.

  • 1
    How do you start the session in your script? – Kusalananda Sep 24 '17 at 16:34
  • @Kusalananda screen -S "name of server" CodeToRunServer. I've tried with -d and -m as well, with no success. – Zebiano Sep 24 '17 at 21:40
0

Maybe you application doesn't really require the screen and can run even without active session (daemon like). Then it can simply detach from the screen, which actually finishes. You may need to specify some parameter to make it running in foreground (if there is such a option).

You may try to run the bash in the screen

screen -dmS script bash -c '/execute/shell/script.sh; exec bash'

That way the screen should survive (with bash) so you can see the console.

  • But that sorta defeats the purpose of it. I know I can type screen -S "name of server" and then run my script for the server. But I'd rather have all in one so I only have to write one command: sudo sh NameOfScript.sh. This should open a new screen session that runs my server (which it does) but I can't access it again! – Zebiano Sep 24 '17 at 21:44
0

First start a new screen session using:

screen -dmS <sessionname> <script_name>

This which will create a new detached screen session with the made-up <sessionname>. It will automatically be detached by default.

Later you can ATTACH to this session whenever you want, by using:

screen -r <sessionname>`

In order to reattach to the running session.

Note: Please use the keyboard combination of: CTRL + A followed by D to DETACH screen again.

Detaching means you keep running the script within screen, without killing the script. Meaning you can reattach whenever you want later again.

  • Please, if you like this answer. Please mark this answer as 'Accepted`. – danger89 Apr 4 '18 at 14:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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