How can I get screen to execute a command and then detach (That is, automatically in a single script without further input beyond initially starting the script)? e.g. I run myscript.sh and it automatically starts a screen session, executes a command, then detaches.


This is an easy one:

screen -d -m yourcommand
  • 4
    no dice. screen -d -m command, screen -list says no sockets, screen -r no sessions – darkfeline Jul 29 '12 at 2:24
  • 1
    Somehow your command wasn't found, or isn't working correctly in the automatically-created screen environment. Try just doing screen yourcommand without the -d and -m and see how that goes first. – Alan Curry Jul 29 '12 at 2:28
  • You're sort of right. screen terminates when the command finishes, contrary to my expectations. But your answer does work. – darkfeline Jul 29 '12 at 2:34
  • 3
    You can change that by enabling the zombie option in screen. Put zombie xy in your ~/.screenrc. It should also be possible to enable it for one session only b putting zombie xy in another file and using -c file but for some reason that's not working when I try it. Or just use sh -c 'yourcommand;while :;do sleep 9999; done' – Alan Curry Jul 29 '12 at 2:39
  • @AlanCurry, Nope, this doesn't work for me either, even though the command runs perfectly (and takes several hours) when run in a screen manually. – Cerin Sep 27 '14 at 3:08

To run a single command in screen and detach, you may try:

screen -dm sleep 10

To run multiple commands, try:

screen -dm bash -c "sleep 10; myscript.sh"

Please note that when a program terminates, screen (per default) kills the window that contained it.

If you don't want your session to get killed after script is finished, add exec sh at the end, e.g.:

screen -dm bash -c 'sleep 5; exec sh'

To list all your sessions, try:

screen -list

Related: Start Unix screen, Run command, Detach.

  • 2
    This worked for me on Ubuntu 16.04. In addition to name your session so you can return to it later, add -S sessionname: screen -dmS MyLongRunningScript bash -c "...". – Dan Dascalescu May 26 '17 at 21:33

In order to start new session in background with name 'sleepy'

screen -S sleepy -dm sleep 60

In order to kill 'sleepy' session

screen -S sleepy -X quit      
  • why doesn't it work like screen -S sleepy -dm "cd myfolder;sleep 60" ? – Toolkit Dec 28 '17 at 9:22
  • @Toolkit The issue is that you have the command in quotes and so it was treated as one large command. Obviously we can't take it out of quotes because of the semicolon. To solve this, execute the command like so: screen -S sleepy -dm bash -c "cd myfolder;sleep 60" – Jason Thompson Mar 1 at 15:51
screen -dmS screen_session_name bash -c 'echo "doing stuff"; exec bash'

it happen to me when I pressed control c (sig int) to exit my program. it exits all the way from all bash. so I found this to catch SIGINT. and prevent exit from last bash. (need to type exit to exit)

screen -dmS "screenNameHere" bash -c "trap 'echo gotsigint' INT; cd /mydir ; my_command_here;  bash"


screen -dmS "status_updates" bash -c "trap 'echo gotsigint' INT; cd /opt/status_update ; forever index.js ;  bash"

I find it useful to use cron to run nodejs programs on startup. and to run the screen at boot time. in cron there are special events syntax @reboot event

to edit cron, execute:
crontab -e

then type
@reboot screen -dmS "screenNameHere" bash -c "trap 'echo gotsigint' INT; cd /mydir ; my_command_here;  bash"

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.