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

5,906

Solution 1

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

Solution 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"
Share:
5,906
dukevin
Author by

dukevin

.-"-.__ | .' / ,-| ```'-, || . ` / /@)|_ `-, | / ( ~ \_```""--.,_', : _.' \ /```''''""`^ / | / `| : / \/ | | . / __/ | |. | | __/ / | | | "-._ | _/ _/=_// || : '. `~~`~^| /=/-_/_/`~~`~~~`~~`~^~`. `> ' . \ ~/_/_ /" ` . ` ' . | .' ,'~^~`^| |~^`^~~`~~~^~~~^~`; ' .-' | | | \ ` : ` | :| | : | |: | || ' | |/ | | : |_/ | . '

Updated on September 18, 2022

Comments

  • dukevin
    dukevin over 1 year

    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
    dukevin almost 13 years
    Thanks! it works perfect. Sorry everyone for being off topic
  • Tanner Strunk
    Tanner Strunk almost 6 years
    does not work for me.