2

I saw many place introduce screen to run background job stably even log out. They use

screen -dmS name

According to screen -h, this option means

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

What is daemon? I don't understand.

I found that if I simply type screen, I can enter automatically into a screen. After I run some command, and press Ctrl+a d, and then log off. The job is still running fine. So is this simple approach OK? Do I really need -dmS to make background job stable?


Let me try to give a summary:

Anything run in screen is safe to logging out (but you should detach the screen, not quit screen when you log out), no matter what the option you have set to screen.

-dmS is just an option convienient for submitting jobs in background noniteractively. That is

screen -dmS nameOfScreen command
6

You would only use -dm if you want to run a command in a screen session and not enter it interactively

-S is just to give the session a usable name so you can reconnect to it again easily later

If you want to use it interactively and don't want to give it a human readable name, you can omit all of those arguments safely.

For example, if you just want to start up screen to run the command, say, /path/to/longTime and you don't want to watch it run you could do it either as

screen -dmS longSession /path/to/longTime

or you could do

screen -S longSession
$ /path/to/longTime

ctrlad

Both would accomplish the same thing, but one is both easier to script and a bit less typing.

  • Hi, Eric Renouf. I don't quite understand "You would only use -dm if you want to run a command in a screen session and not enter it interactively" . -dm mean detached, how could I run command without enter it? If I enter the screen, then it is interactive. Am I right? – user15964 Apr 29 '16 at 14:51
  • @user15964 you can give the command to execute within the screen session on the command line and never interactively be in the screen session. That's what -dm helps do. So no, you do not have to enter screen interactively, you can give it the command you want to run without typing it within the session directly – Eric Renouf Apr 29 '16 at 14:54
  • @user15964 see my update to hopefully be a little clearer how the two options compare – Eric Renouf Apr 29 '16 at 14:58
  • Thank you so much. I tried to write a summary, to see if I am wrong somewhere : ) – user15964 Apr 30 '16 at 1:42
3

It's more useful as screen -dmS name command args - that will start a screen session called name in the background (i.e. detached), and run command args inside that screen session.

Without the command args, it will just start the screen session in the background.

In either case, you can attach to that session later with, e.g., screen -d -r name. From man screen:

-d|-D [pid.tty.host]

does not start screen, but detaches the elsewhere running screen session. It has the same effect as typing "C-a d" from screen's controlling terminal. -D is the equivalent to the power detach key. If no session can be detached, this option is ignored. In combination with the -r / -R option more powerful effects can be achieved:

-d -r Reattach a session and if necessary detach it first.

-d -R Reattach a session and if necessary detach or even create it first.

-d -RR Reattach a session and if necessary detach or create it. Use the first session if more than one session is available.

-D -r Reattach a session. If necessary detach and logout remotely first.

-D -R Attach here and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user. This is the author's favorite.

-D -RR Attach here and now. Whatever that means, just do it.

Note: It is always a good idea to check the status of your sessions by means of screen -list.

That man page is missing an important detail here - it should read:

-d|-D [pid.tty.host|session name]

  • Thank you very much for detailed information, very useful : ) – user15964 Apr 30 '16 at 1:45
1

-dmS just enforces these things:

  1. It will start detached. -dm
  2. And you can name your session. -S sessioname

Running as a daemon indicates that it will execute the specified command in the background until execution finishes.

Starting screen, then running the command, then detaching should have the same behavior as using -dmS to run the command. I often run game servers or long jobs by starting screen, then detaching with Ctrl-A+d

You can also check the man pages man screen for more in depth info on the options.

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.