Shell Gnu-Screen -X Stuff Problems

Shell GNU-Screen -X Stuff problems

Adding the argument -p 0 should fix it. Something like this:

screen -x $PROCESS -p 0 -X stuff `printf "stop\r"`

(From screen documentation: -p window Preselect the named window if it exists.)

Can't stuff literal $$ into gnu screen window

Put single quotes around the whole thing, instead of double quotes. Double quotes still do expansion inside, single quotes don't.

BTW if you need to escape things in the middle of a string, you can break it like "foo "'something'" bar"

GNU Screen: Can't stuff commands unless the screen is attached?

When you start a Screen session in detached mode (screen -d -m), no window is selected, so input later sent with screen -X stuff is just lost. You need to explicitly specify that you want to send the keystrokes to window 0 (-p 0). This is a good idea anyway, in case you happen to create other windows in that Screen session for whatever reason.

screen -S "$1" -p 0 -X stuff "$beast$(printf \\r)"

(printf \\r to strictly emulate the Return key; many but not all programs accept a newline (\n).)

Sending commands to screen: limits of stuff, quirks w/ newlines

The screen man page suggests that stuff should not be used for large strings. As an alternative you can read a file into a register (a sort of buffer) and then paste it. Here, p is the arbitrary register:

screen -S worker -X readreg p /foo/geo_command.txt
screen -S worker -X paste p

This more direct method has the advantage that you don't need to go via an intermediate shell variable, as in GEO_COMMAND="$(cat ...)", which loses the final newline. Also, the data is not interpreted by screen (e.g. the 2 characters \n in the file is not replaced by a 1 character newline).

Unable to send Enter-Stroke to screen session from script

Just found the answer myself.
In case someone stumbles on the same problem: Instead of using echo -ne '\015' simply put a ^M at the end of the command.

New script:

#!/bin/sh

clear

while :
do
screen -S SessionA -X stuff '^C'
screen -S SessionA -X stuff 'java -jar jarFile.jar^M'

sleep 30
done

Send commands to a GNU screen

If the Screen session isn't running, you won't be able to send things to it. Start it first.

Once you've got a session, you need to distinguish between Screen commands and keyboard input. screen -X expects a Screen command. The stuff command sends input, and if you want to run that program from a shell prompt, you'll have to pass a newline as well.

screen -S demo -X stuff '/home/aa/scripts/outputs.sh
'

Note that this may be the wrong approach. Are you sure you want to type into whatever is active in that session? To direct the input at a particular window, use

screen -S demo -p 1 -X stuff '/home/aa/scripts/outputs.sh
'

where 1 is the window number (you can use its title instead).

To start a new window in that session, use the screen command instead. (That's the screen Screen command, not the screen shell command.)

screen -S demo -p 1 -X screen '/home/aa/scripts/outputs.sh'

How can I send stuff commands to a start-in-detached screen?

Edit: The best answer to this question is this other SO answer. I leave my kludgey solution here, anyway, in case it inspires a solution to a similar problem.


A possible workaround is to use a second, already running and detached screen session to start the screen session to which you want to send the "stuff" command in attached mode, and then send that screen session a detach command and then the stuff command.

$ screen -dmS spawner
$ screen -S spawner -X screen screen -dR mydaemon
$ sleep 1 # may be necessary
$ screen -S mydaemon -X detach
$ screen -S mydaemon -X stuff "whatever"

(Note: the doubled "screen" is not a typo!) You are still left with an unstuffable screen session (spawner) but if the resources it takes are important you can always just use "kill -TERM ..." or its ilk to terminate it (or have it automatically exit after a certain amount of time by starting it with something like

$ screen -dmS spawner bash -c "sleep 60"

or similar).



Related Topics



Leave a reply



Submit