How to execute a command in screen and detach?

82

27

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.

darkfeline

Posted 2012-07-29T02:04:52.273

Reputation: 1 103

Answers

120

This is an easy one:

screen -d -m yourcommand

Alan Curry

Posted 2012-07-29T02:04:52.273

Reputation: 2 354

@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 – 2014-09-27T03:08:50.523

is there any updated answer? – Oki Erie Rinaldi – 2016-03-08T13:53:04.097

4no dice. screen -d -m command, screen -list says no sockets, screen -r no sessions – darkfeline – 2012-07-29T02:24:58.367

1Somehow 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 – 2012-07-29T02:28:01.387

You're sort of right. screen terminates when the command finishes, contrary to my expectations. But your answer does work. – darkfeline – 2012-07-29T02:34:32.643

4You 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 – 2012-07-29T02:39:15.400

49

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.

kenorb

Posted 2012-07-29T02:04:52.273

Reputation: 16 795

2This 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 – 2017-05-26T21:33:51.090

Is there a way to replace 5 in screen -dm bash -c 'sleep 5; exec sh' by a variable? – Radio Controlled – 2020-01-08T08:43:29.597

@RadioControlled time=5; screen -m bash -c "sleep $time; exec sh" – kenorb – 2020-01-08T10:21:23.177

@kenorb, the problem is that it only worked with single quotes. Let's say: 'for k in \seq $i $j`; do echo $k; done'`, where $i and $j are from the parent script. – Radio Controlled – 2020-01-08T12:37:40.123

16

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      

Michal Zmuda

Posted 2012-07-29T02:04:52.273

Reputation: 261

why doesn't it work like screen -S sleepy -dm "cd myfolder;sleep 60" ? – Toolkit – 2017-12-28T09:22:16.107

@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 – 2019-03-01T15:51:56.973

3

screen -dmS screen_session_name bash -c 'echo "doing stuff"; exec bash'

tkjef

Posted 2012-07-29T02:04:52.273

Reputation: 131

0

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"


example:

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"

Shimon Doodkin

Posted 2012-07-29T02:04:52.273

Reputation: 101