Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a detached screen, send a command to it

I'm trying to do something which is proving suprisingly difficult. I want to create a screen session without attaching to it (since this will eventually become a startup script), then send a bash command to the session.

I've tried to simply echo Hello in a newly created session. The screen session is created fine but the echo never happens. Given the following example, I would expect to finally attach to a screen which has "Hello" on it's console:

screen -mdS "Test" # Create a screen session, do not attach to it
screen -ls # Confirm that the Test screen session exists
screen -S "Test" -X "echo Hello^M" # Send a command through
screen -R # Reconnect - notice the command didn't execute

But there's nothing in the session at all - the echo was not executed. Any pointers are hugely appreciated?!

like image 767
ledneb Avatar asked Apr 10 '16 18:04

ledneb


People also ask

How do you attach a screen command?

On the command prompt, type screen . Run the desired program. Use the key sequence Ctrl-a + Ctrl-d to detach from the screen session. Reattach to the screen session by typing screen -r .

How do you detach a screen?

To detach it, type Ctrl-a Ctrl-d (most commands in screen start with Ctrl-a, this overrides the Ctrl-a command normally used when you want to jump to the start of a line).

How do I connect to a detached screen session?

To start a screen session, you simply type screen within your ssh session. You then start your long-running process, type Ctrl+A Ctrl+D to detach from the session and screen -r to reattach when the time is right. Once you have multiple sessions running, reattaching to one then requires that you pick it from the list.

How do I run a command in screen and detach?

To run a single command in screen and detach, you may try: To run multiple commands, try: 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.:

How to start a detached Screen session in Linux?

tells screen to log the output to a text file named filename.txt. Shell commands are things you type in your terminal. Both commands like ls or keyboard shortcuts like CTRL+C. You can start a detached screen session with the following command: you can view all screen sessions and with either screen -r or screen -x you can reattach to a session.

How do I send a screen command to a session?

To send a screen command to a session: The -p 0 flag is for the window inside screen. If you have created multiple windows ( CTRL+A c) you can specify the number. With CTRL+A [0-9] you can directly go to that window inside screen. For screen commands, after the -X flag you don't need quotes.

How to detach a screen from a Windows 10 PC?

It can also be done with the help of shortcut key Ctrl-a + d Here 1643 is the screen id we want to detach. -r: It is used to reattach a screen session which was detached in past.


1 Answers

The correct invocation is

screen -S "Test" -X stuff 'echo Hello\r'

like image 106
n. 1.8e9-where's-my-share m. Avatar answered Sep 28 '22 05:09

n. 1.8e9-where's-my-share m.