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

10

2

I have the following script

screen -d -m -S $1                #start screen minimized
screen -S $1 -X stuff "$beast     
"                                 #stuff commands

unless I attach the screen first it seems the affects of stuff won't do anything? Why is this and how can I modify this script so I don't need to attach the screen?

dukevin

Posted 2011-06-02T08:39:37.223

Reputation: 524

Answers

14

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).)

Gilles 'SO- stop being evil'

Posted 2011-06-02T08:39:37.223

Reputation: 58 319

does not work for me. – Tanner Strunk – 2018-06-10T05:38:43.240

Thanks! it works perfect. Sorry everyone for being off topic – dukevin – 2011-06-02T08:56:12.847

2

This recently came up while trying to answer a question on unix.stackexchange.com.

The summary is that screen does not have a default selected window unless you attatch, but Gilles showed us how you can force one to be selected by adding the argument -p 0 to your screen command.

Personally I recommend switching to tmux. Here is how you would port your screen commands to work in tmux:

tmux new-session -d -n $1
tmux send-keys -t $1 "$beast\n"

Caleb

Posted 2011-06-02T08:39:37.223

Reputation: 4 323