Create screen and run command without attaching

109,521

Solution 1

I think you may be running into several issues.

If the command finishes before you re-attach, the screen will go away. You can demo this by using:

screen -d -m ls -l

It will run the ls -l command, but screen -list won't show it as the screen process has ended.

I also have no idea what you are trying to do with this \015 stuff. Perhaps updating your question would help, as what I think you're trying to do is run multiple commands in a screen session. This should be as simple as:

screen -d -m bash -c "command1 ; command2 ; command3"

If these are used a lot, perhaps you should make a shell script that runs just these commands, then use a more simple:

screen -d -m /path/to/script.sh

Solution 2

Start a detached screen

screen -dmS <screen name>

Execute command in previously created detached screen

screen -S <screen name> -X stuff '<CMD>\n'

Yes you need to type the enter symbol to submit the command or else it will just add the string to the screen.

http://osdir.com/ml/gnu.screen/2003-09/msg00029.html

Solution 3

This did the job for me, without -c wasn't working

screen -d -m bash -c "command1; command2; command3"

Solution 4

I've encountered this problem before, it was a bug with the cygwin implementation.

What I did was create a ".screenrc_detaching" having only the following command

#detach
detach 

and then start screen with

screen -c ~/loginScripts/tempScreenrc/.screenrc_detaching

Then you have your screen session and its already been attached and detached and you're good to pump commands to it.

Solution 5

A copy'n'paste way to test previous answers is:

 # No sessions:
screen -ls
 # Returns immediately:
time screen -dmS screen_descritive_session_name  bash -c 'sleep 20; hostname >> /tmp/h'
 # New session present:
screen -ls
 # File with return of command was created :)
sleep 20; cat /tmp/h

The expect result should be similar to:

No Sockets found in /var/run/screen/S-yourusernamehere.

(That means no screen-session was previously created)

real    0m0.002s
user    0m0.000s
sys     0m0.000s

(It's the time spent to create screen and detach from it. Almost instantaneous.)

There is a screen on:
    20318.screen_descritive_session_name    (20/08/2018 16:29:35)   (Detached)
1 Socket in /var/run/screen/S-yourusernamehere.

(This output show screen sessions available. Created on last command.)

sleep 20; cat /tmp/h

(This cat shows the hostname executed inside gnu-screen)

Share:
109,521

Related videos on Youtube

coffeecoder
Author by

coffeecoder

Updated on September 17, 2022

Comments

  • coffeecoder
    coffeecoder about 17 hours

    I am working on automating a maintenance routine that involves starting and stopping a script that is running in a screen session. My approach is to kill the screen session, and then restart it and run the command from within a script using the abilities to both create a screen and pass a command without needing to attach to the screen.

    However, I am having difficulties with this. I can create the screen correctly without it attaching using screen -d -m -S screen_name. However, if I run a command based on:

    screen -S screen_name-X stuff "command 1"'echo -ne '\015''"command 2"'echo -ne '\015''

    with the echo -ne '\015' being wrapped with backticks rather than single quotes. It is to simulate the user pressing the enter key as the commands I use are moving to a directory and executing a script located there. This command works, but only if the screen has been attached to once it has been created. As I am trying to automate the process of creating the screen and running the commands within it I would like to avoid having to attach and detach within a script. I will be trying the suggestion of creating a shell script containing the commands I need to execute within the screen and edit according to my results.

    Is there a way to create a screen and run a command within the screen either in one command, or without having to attach to the screen after creating but before execution of the command?

    Thanks in advance.

    **Update - having tried the suggestion to place the commands I need to execute within a shell script I have been able to successfully create a screen and execute the commands from within the screen, but I am getting the behaviour that when the script stops running the screen closes as well. This shouldnt be a problem as the script is a logging script that should only stop with the knowledge of the sys admin or through the script I am trying to develop, however it would be preferable to have the screen setup in such a way that the screen does not disappear if the script is stopped. Is it possible to achieve this behaviour? **

    • Dan Herbert
      Dan Herbert about 10 years
      I ran into this same problem and found a solution on superuser.com for anyone else who stumbles upon this problem superuser.com/questions/342463/…
    • gwyn
      gwyn almost 10 years
      To keep the screen open you could use something like: screen bash -c 'echo "test"; /bin/bash'
    • Martin C.
      Martin C. about 8 years
      For your auto-closing issue, you could use a special .screenrc that contains the line zombie kr, which will keep a finished window open, and you can press k to close the winodw, or r to run the command in the window again. I have this for my default .screenrc.
  • bright-star
    bright-star over 8 years
    is there a way to get similar behavior without the screen detaching immediately? Say I want to start an ssh session and then detach right after; screen -d -m ssh will detach at the password prompt before the session is made.
  • Cerin
    Cerin almost 8 years
    This doesn't work for me. Replacing hte "commandN" with several long-running programs results in nothing showing up in screen -list. Also, this doesn't name the screen as the OP is trying to do.
  • Dan Ciborowski - MSFT
    Dan Ciborowski - MSFT almost 7 years
    I was looking to run a python program using this method. I tried to create a sh script to run it, but did not see the screen in the list. Instead I used screen -d -m python EventGenerator.py, which worked great
  • marcovtwout almost 6 years
    That should probably be bash -c "commands" instead of bash "commands"
  • Xdg
    Xdg almost 6 years
    You're right, there has to be -c .
  • Karl Morrison
    Karl Morrison over 4 years
    Would be nice to hear the flags mean?
  • Pierre.Vriens
    Pierre.Vriens about 4 years
    and what is the result of such tests?
  • Enrique S. Filiage
    Enrique S. Filiage about 4 years
    You are right, there was no example. I will add.
  • Pierre.Vriens
    Pierre.Vriens about 4 years
    much better! merci! +1
  • Nathan B
    Nathan B over 3 years
    but how to leave the screen open after the command has finished?
  • Nathan B
    Nathan B over 3 years
    what is 'stuff' ?
  • alisa
    alisa almost 3 years
    stuff is actually a command that copy-pastes another command into a screen session: stuff [string] Stuff the string string in the input buffer of the current window. This is like the "paste" command but with much less overhead. Without a parameter, screen will prompt for a string to stuff. You cannot paste large buffers with the "stuff" command. It is most useful for key bindings. See also "bindkey". [cited from theterminallife.com/sending-commands-into-a-screen-session/]