0

What does 'stuff' means in the example in answer from How to run a program in a screen, redirect all output to a file and detach, because the command is after in the $'' area.

screen -S workspace -X stuff $'ps aux > output-x\n'

How to run a program in a screen, redirect all output to a file and detach

2 Answers2

0

According to comment:

stuff sends characters, exactly as if you had typed them

This seems to only work if the session was attached at some point, i.e. on creation. Also since it sends characters, sending the newline is required for executing the command.

For Linux, the steps that worked for me:

  1. screen -S some_session_name
  2. Ctrl+A D to detach
  3. screen -S some_session_name -X stuff 'command'$(echo -ne '\015')

I got the instructions from this "Terminal / Life" post: Sending Commands into a Screen Session.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Jeff
  • 26
0

From the manual:

Stuff the string string in the input buffer of the current window.

The session definitely does not need to be attached ever.

Let's break it down:

  1. gnu screen is a program which enables you to disconnect from running programs, and then reconnect to them later on.
  2. screen -dmS workspace creates a new screen that is already disconnected.
  3. screen -S workspace -X [command] will send [command] to the screen session called workspace.
  4. [command] is not a bash command; it's just an internal command for screen... the same as pressing CTRL+A,: when connected to a screen session normally.
  5. Other example [command] are detach or sessionname myNewName.
  6. The [command] chosen by user was stuff - which just "pastes text" into it's session.
  7. stuff is expecting a string as a parameter... so if you submit stuff "hello" and then re-connected to your screen session: you will see the word hello typed in with the blinking cursor after it...
  8. You can trick screen into executing a text you "pasted" in, by putting a line-return character at the end of your text (such as \n or ^M).
  9. So now, if you changed your "pasted" string from "hello" to "hello^M" your full command would look like this: screen -S workspace -X stuff "hello^M" ... and now when you re-connect to your screen you will see it says Command 'hellohello' not found..
  10. Now, we just replace our pasted text hello with whatever we want to execute!

Example:

screen -ls
> "No Sockets found."

screen -dmS workspace
screen -ls
> "There is a suitable screen on..."

screen -S workspace -X stuff "echo Hello World! PID $$ > hello.txt^M"
cat hello.txt
> Hello World! PID 1234
Hicsy
  • 101