Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 where I thought things would be: how many screen sessions there are (game has 4 windows in byobu and lordquackstar has 2 windows)

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?

share|improve this question
    
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 Jan 8 '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 8 '11 at 13:38

2 Answers 2

up vote 2 down vote accepted

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.

share|improve this answer
    
You can simply include a newline at the end of the string to stuff. –  Gilles Jan 8 '11 at 14:05
    
Whats "stuff"? Removing it gives the error in byobu "-X Unknown command 'uanme -a ' –  TheLQ Jan 10 '11 at 21:05
    
It does what it says, it "stuffs" the command (in this case uname -a) into the byobu session. –  wag Jan 10 '11 at 21:11
    
Ah, didn't make the connection there. Anyway, thanks for the help, it works perfectly! –  TheLQ Jan 10 '11 at 22:25
    
The stuff command only seems to work with GNU screen, not tmux, which seems to be the default lately. –  Replete Dec 5 '13 at 15:31

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
'
share|improve this answer
    
It works for me when I stuff a string with no space, but apparently doesn't do anything. Any ideas? –  François Beausoleil Apr 11 '13 at 18:09
    
OK, my bad. The space was interpreted by bash locally, and SSH wasn't seeing the full command. Works now! –  François Beausoleil Apr 11 '13 at 18:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.