9

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?

Anwar
  • 123
TheLQ
  • 1,406
  • It looks like each user has a single screen session. I'm not familiar with byobu, why do you think there are five screen sessions? – Gilles 'SO- stop being evil' Jan 08 '11 at 13:23
  • @Gilles More research found out that there is only one screen session for each user. I thought their where more since htop showed multiple screen processes – TheLQ Jan 08 '11 at 13:38

2 Answers2

7

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.

wag
  • 35,944
  • 12
  • 67
  • 51
7

You can send a command to a particular screen window of a particular screen session without attaching to it.

screen -S sessionname -p windowname -X screencommand

The session name is set with the -S option when starting screen or the sessionname command; by default it's byobu with byobu. You can also use the screen PID after -S. You can set a window's name with the title command. You can also use the window number.

screen -S byobu -p 1 -X stuff 'ls
'