bash gnu-screen byobu – Connect to Byobu Screen Session and Execute Command

bashbyobugnu-screen

In a script I am building I'm experimenting with how to automate as much as possible. One of the more interesting challenges is to connect to a byobu screen session and execute a command.

So I started in the obvious place, looking on how many screen sessions there are (game has 3 windows in byobu and lordquackstar has 2. The users are in separate putty instances)

game@quackgame:~$ screen -ls
There is a screen on:
        2019.byobu      (01/05/2011 05:10:04 PM)        (Attached)
1 Socket in /var/run/screen/S-game.

Only one there, so I checked for the system

lordquackstar@quackgame:/home/game$ sudo ls -lAR /var/run/screen/
/var/run/screen/:
total 0
drwx------ 2 game          users         100 2011-01-06 09:18 S-game
drwx------ 2 lordquackstar lordquackstar 100 2011-01-06 09:17 S-lordquackstar

/var/run/screen/S-game:
total 4
prwx------ 1 game users 0 2011-01-08 07:55 2019.byobu
-rw------- 1 game users 0 2011-01-06 09:18 byobu-exchange
-rw-r--r-- 1 game users 3 2011-01-08 07:32 byobu.updates-available

/var/run/screen/S-lordquackstar:
total 4
prwx------ 1 lordquackstar lordquackstar 0 2011-01-08 07:42 1169.byobu
-rw------- 1 lordquackstar lordquackstar 0 2011-01-06 09:17 byobu-exchange
-rw-r--r-- 1 lordquackstar lordquackstar 3 2011-01-08 07:35 byobu.updates-available

Still no multiple screens

So for my question: How can I connect to a window in byobu from a script?


On a slightly related note, once I connect to it from a bash script, is there any way to send it a command then detatch?

Best Answer

You can directly attach to a previously detached byobu/screen session including the window:

byobu -r -p2

will reattach into window 2 (or a named one).

-X can send any command to a byobu/screen session and also works with the -p switch.

byobu -p2 -X stuff "uname -a $(echo -ne '\r')"

This will send a uname -a to the second (third actually) byobu window, the echo at the end sends a carriage return so the commands gets executed.

Related Question