Friday, July 3, 2009

Sending Console Input to a Detached screen Session

The following will send console input to a specific window in a screen session (which may be detached), as if it was typed interactively:
screen -S session-id -p window-id -X stuff "command^M"

The script snippet below demonstrates this by launching a new screen session in detached mode, creating a second window in that session and then executing a different command in each window:
screen -dm
screen -X title shell0
screen -X screen -t shell1
screen -p 0 -X stuff "echo hello window 0^M"
screen -p 1 -X stuff "echo hello window 1^M"

Note that ^M stands for the newline charcter, which you can generate, in emacs or in a terminal running bash, by hitting <CTRL>-q and then <CTRL>-m or <ENTER>.

1 comment: