Create screen and run command without attaching

Posted on

A server stack is the collection of software that forms the operational infrastructure on a given machine. In a computing context, a stack is an ordered pile. A server stack is one type of solution stack — an ordered selection of software that makes it possible to complete a particular task. Like in this post about Create screen and run command without attaching was one problem in server stack that need for a solution. Below are some tips in manage your linux server when you find problem about linux, gnu-screen, , , .

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 '15''"command 2"'echo -ne '15''

with the echo -ne ‘15’ 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? **

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 15 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/

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.

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

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

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)

Leave a Reply

Your email address will not be published. Required fields are marked *