How can screen be used to start a new session and run a command within that session without killing that session?

 screen -S myScreenName runSomething

I tried the above command and it says

[screen is terminating]

Did it run the command then kill the screen? I would like the screen to be detached after running the command instead of killing it. How can that be done?

  • 1
    Why are you using screen here at all, if the command is just going to exit immediately? Feb 27, 2014 at 2:29
  • @MichaelHampton the command doesn't exit immediately, it runs a daemon that prints to stdout, so I would like to keep it in a screen session. I will be starting the screen session from an Ansible playbook. I don't know why screen terminates when I ran that command.
    – Nyxynyx
    Feb 27, 2014 at 2:32
  • 1
    Then tell the daemon not to daemonize. Feb 27, 2014 at 2:35
  • @MichaelHampton Great that works. Can screen be asked to detach after running the command? Tried -d but it says There is no screen to be detached matching myScreenName
    – Nyxynyx
    Feb 27, 2014 at 2:50

3 Answers 3


This script prevent session killing:

screen -dmS test bash -c 'echo waiting 5 senconds...; sleep 5; exec bash'


  • It seems that starting a session name with . is not allowed (tried to give a relative file path as both session name and one of the arguments).
    – Luc
    Feb 16, 2021 at 14:41

to run a command and detach from the session:

screen -d -m runSomething
  • The screen still terminates when using this command.
    – Luc
    Jan 4, 2015 at 23:34

If you keep forgetting the right letters that go after dash, in order to avoid revisiting this question, you might as well type:

screen -h

where you'll see:

-dmS name     Start as daemon: Screen session in detached mode.

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