179

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.

kenorb
  • 24,736
  • 27
  • 129
  • 199
darkfeline
  • 2,072
  • 2
  • 14
  • 15
  • It is important to note that for many of these answers, screen will automatically terminate your screen when the command is done running and the program exits. So, you might think it didn't work, but you are likely just not seeing it because it terminated. That is why there is a `sleep` or `exec` command in answers below, to force screen to not terminate. – Akaisteph7 Jul 29 '22 at 18:49

8 Answers8

222

This is an easy one:

screen -d -m yourcommand

From the Screen User's Manual:

-d -m
Start screen in detached mode. This creates a new session but doesn’t attach to it. This is useful for system startup scripts.

chucknelson
  • 117
  • 6
Alan Curry
  • 3,484
  • 1
  • 17
  • 9
  • 11
    no dice. screen -d -m command, screen -list says no sockets, screen -r no sessions – darkfeline Jul 29 '12 at 02: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 02:28
  • 1
    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 02:34
  • 6
    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 02:39
  • 1
    @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 03:08
  • is there any updated answer? – Oki Erie Rinaldi Mar 08 '16 at 13:53
  • Use this to start my android emulator because Android Studio didn't seem to run emulator on GPU: "screen -d -m emulator -avd Pixel_3_API_29_2 -gpu host" – bikram Aug 14 '20 at 09:23
  • @Cerin in my case, I was doing something like `screen -d -m " --arg1 arg1_value --arg2 arg2_value"` and it did nothing, but removing the double quotes worked for me. – user1442960 Aug 25 '21 at 17:01
  • Add `-S screennamehere` to give the screen an identifiable label that can be used for `screen -r` later. – DustWolf Sep 30 '22 at 13:08
103

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
  • 24,736
  • 27
  • 129
  • 199
  • 8
    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
  • Is there a way to replace `5` in `screen -dm bash -c 'sleep 5; exec sh'` by a variable? – Radio Controlled Jan 08 '20 at 08:43
  • 1
    @RadioControlled `time=5; screen -m bash -c "sleep $time; exec sh"` – kenorb Jan 08 '20 at 10:21
  • @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 Jan 08 '20 at 12:37
  • This is a robust answer. I needed `bash -c`. I can only assume that when I called `screen -dm head foo > bar` it wrote `screen -dm head foo` to `bar`. `screen -dm bash -c "head foo > bar"` fixed that. – DryLabRebel Apr 03 '23 at 06:18
34

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
  • 441
  • 4
  • 5
  • why doesn't it work like `screen -S sleepy -dm "cd myfolder;sleep 60"` ? – Toolkit Dec 28 '17 at 09:22
  • 1
    @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 01 '19 at 15:51
9
screen -dmS screen_session_name bash -c 'echo "doing stuff"; exec bash'
tkjef
  • 191
  • 1
  • 4
4

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"
  • 1
    exactly what was looking for -- had same issue where shell would exit -- thanks! P.S. and screen -r SessionName -x {quit, kill} can be used later if needed – DotDotJames Apr 12 '20 at 20:00
2

Here are the steps you can follow to run a process in screen, detach from the terminal, and then reattach.

  1. From the command prompt, just run screen. This will give you a new subshell.

  2. Run your desired program

  3. Detatch from the screen session using the key sequence Ctrl-a Ctrl-d (note that all screen key bindings start with Ctrl-a). This will drop you back to your original shell and display a message "[detached]", indicating that the screen session is still running.

  4. You can then list the available screen sessions by running screen -list

  5. You can reattach to this screen session by running screen -r. Once reattached, you will be able to take off where you left off and see any output that was printed to the screen during the time that you were detached. If you have multiple screen sessions, then you can specify the tty name (as displayed by screen -list) as an argument to screen -r to attach to a particular session.

Source

Vinnie James
  • 121
  • 3
0

Buliding on some of the ideas in other answers here, and on https://serverfault.com/questions/368054/run-an-interactive-bash-subshell-with-initial-commands-without-returning-to-the my soloution was.

screen -dm bash -c 'bash --init-file <(echo command)'

replacing command with the command to execute.

This is the closest equivilent I can think of to manually starting a screen session and then running a command inside it.

ctrl+c works as it normally would in an interactive session to kill the program and return to a shell inside screen. Unfortunately ctrl+z doesn't seem to work.

plugwash
  • 5,994
  • 2
  • 18
  • 25
-1

I hope this may help for screen issues: https://askubuntu.com/questions/124897/how-do-i-detach-a-screen-session-from-a-terminal/1429444#1429444

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 15 '22 at 07:16
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/1146038) – DarkDiamond Sep 15 '22 at 07:50
  • Thank you for pointing out that I have shared the wrong link earlier. – Brijesh Sondarva Oct 31 '22 at 10:24