Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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.

share|improve this question
The way you are invoking screen, you only create one window to run a single command. When the command exits, the window closes and screen has nothing left to do, so it exits. – jw013 Sep 5 '12 at 20:39
So... is there a way to open two windows (using command line only, no keyboard combinations), one to execute a script and second to prevent screen from closing? BTW: I still don't get the idea behind! If I call screen -dmS name, I create a detached window, which also has nothing left to do. But it isn't automatically closed! But when I want to execute something in that detached screen, it ends once execution is done. Why? I don't see logic here. Can have window doing nothing, but can't have window doing something and then doing nothing? – trejder Sep 5 '12 at 20:45
When you start a window without specifying a command, it is not "doing nothing". The "something" it is doing is running an interactive shell, which does not exit until you exit it manually. – jw013 Sep 5 '12 at 20:49
Cool! Thanks for an explanation! So... back to the question... is there any way to run a command inside a detached screen/window and NOT to end that session, once command execution ends? Also, please take a look at updated question. Thank you. – trejder Sep 5 '12 at 21:00
This script is executing two UDP listeners written in PHP. Though both are executed as daemons (with & at the end), due to buggy server (QNAP), they're are terminated when session ends (I was told, they shouldn't on normal Linux, if executed with &). So I'm not looking to do anything after start_udp_listeners.sh finishes, only how to prevent session from being terminated. As long as it isn't, my PHP listeners are working just fine. – trejder Sep 5 '12 at 21:11
up vote 20 down vote accepted

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):

screen -dmS session_name sh -c '/share/Sys/autorun/start_udp_listeners.sh; exec bash'
  • -dm: starts screen in detached mode
  • -S: sets session name for screen for easier retrieval later on
  • sh -c '...': instead of simply running your script, which will terminate, use sh -c to run multiple commands
  • exec bash: after the script terminates, the sh from above will switch over to an interactive shell (bash), which should never exit until something external terminates it. This will keep screen open as long as the bash instance is alive.
share|improve this answer
Hm... seems you're my life saviour! :] Extending your solution with export IGNOREEOF=1 to prevent accidential termination with Ctrl+D should give me the perfect solution, I've been looking whole long evening. THANKS! :] – trejder Sep 5 '12 at 21:34
This doesn't work on Debian as of today, -c is an argument to read an alternative .screenrc file... – ToVine Jan 31 '15 at 22:44
@ToVine works perfectly on Debian Stable (Wheezy). I used bash -c though. – TranslucentCloud Apr 9 '15 at 9:42
@TranslucentCloud did you enclose the whole command "bash -c ..." in quotes? The last time I tried it (right before writing the comment) it didn't work for me, not quite sure why... – ToVine Apr 14 '15 at 14:12
@ToVine here is my whole command: screen -LS sauf bash -c "sudo aptitude update && sudo aptitude full-upgrade; exec bash" – TranslucentCloud Apr 14 '15 at 16:24

I had no luck with sh -c on my raspberry pi 2 running Debian 7.8. But bash -c did the job:


/usr/bin/screen -dmS test-screen bash -c "/usr/bin/top; exec bash"
share|improve this answer
Could this be, because you're using (on Raspberry Pi) a different shell? The bash and sh seems to be two different shells. That's all, I think, but I may be wrong, since I'm newbie to Linux at all. – trejder Jan 14 at 7:30
sh is the default unix-shell, it is included in raspbian (debian). The command sh also works, but not sh -c in combination with screen. – scotty86 Jan 20 at 1:50

Your Answer


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.