Connect to byobu screen session and execute command?
Solution 1
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.
Solution 2
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
'
Related videos on Youtube
TheLQ
Updated on September 17, 2022Comments
-
TheLQ over 1 year
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?
-
Admin over 13 years@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
-
-
Gilles 'SO- stop being evil' over 13 yearsYou can simply include a newline at the end of the string to stuff.
-
TheLQ over 13 yearsWhats "stuff"? Removing it gives the error in byobu "-X Unknown command 'uanme -a '
-
wag over 13 yearsIt does what it says, it "stuffs" the command (in this case
uname -a
) into thebyobu
session. -
TheLQ over 13 yearsAh, didn't make the connection there. Anyway, thanks for the help, it works perfectly!
-
François Beausoleil about 11 yearsIt works for me when I stuff a string with no space, but apparently doesn't do anything. Any ideas?
-
François Beausoleil about 11 yearsOK, my bad. The space was interpreted by bash locally, and SSH wasn't seeing the full command. Works now!
-
Phil Ricketts over 10 yearsThe stuff command only seems to work with GNU screen, not tmux, which seems to be the default lately.