I'm trying to force GNU screen to create a "virtual" terminal, without attaching to it, execute script inside and NOT terminate session once script ends.
I tried many combinations, including:
screen -dmS udplistener /share/Sys/autorun/start_udp_listeners.sh
or
screen -S udplistener -X /share/Sys/autorun/start_udp_listeners.sh
and none of them worked. I either get session without executed script, script executes, but session is terminated once it finishes or I'm getting "No screen session found" error.
What I'm basically trying to do is to run UDP listener, written in PHP and make it work in infinte loop (don't break listening). Yes — I could run PHP script with &
at the end, forcing PHP CLI to run as daemon. The problem is, that I'm using a piece of junk called server (QNAP — never, ever buy this junk!) which does seems to be ignoring this. As soon as I logoff SSH session, scripts stops.
So screen
seems to be the only option. But I can't understand, why it terminates session once executed command or script ends?
EDIT: I've also tried example found in the Internet:
screen -dmS name
screen -S name -p windowname -X stuff 'mc
'
No lack! After attaching to it (screen -R name
) I see that Midnight Commander HASN'T been executed. Though example author said, it will be.
Best Answer
To keep screen busy after the script completes, just keep something persistent running in a window. The simplest choice for that "something" is probably an interactive shell. Here's one way to do it (assuming
bash
as the choice of interactive shell):-dm
: starts screen in detached mode-S
: sets session name for screen for easier retrieval later onsh -c '...'
: instead of simply running your script, which will terminate, usesh -c
to run multiple commandsexec bash
: after the script terminates, thesh
from above will switch over to an interactive shell (bash
), which should never exit until something external terminates it. This will keepscreen
open as long as thebash
instance is alive.