ledneb
ledneb

Reputation: 1431

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?!

Upvotes: 7

Views: 7162

Answers (2)

richard_ma
richard_ma

Reputation: 156

Try:

screen -S "Test" -X stuff 'echo "Hello"'`echo -ne '\015'`

stuff is a screen command: screen docs for stuff command

`echo -ne '\015'` means pressing Enter key

I found this solution: link

Upvotes: 1

n. m. could be an AI
n. m. could be an AI

Reputation: 120239

The correct invocation is

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

Upvotes: 8

Related Questions