Add helper script to update X11 session environment variables

Bug #908944 reported by Ryan Thompson on 2011-12-27
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
byobu
Wishlist
Unassigned

Bug Description

Generally my byobu sessions have longer lifetimes than my X11 logins. It would be nice to have a way to update all the relevant environment variables so that after I start a new X session, I can still start graphical programs from the command line. This could be added as a startup script so that it gets run when I log in.

Proof of concept for sending new environment variable values to a running screen/tmux session can be found here: https://github.com/DarwinAwardWinner/screen-sendenv

Run the scripts with --verbose to see exactly what commands they are running to update environment variables.

Changed in byobu:
importance: Undecided → Wishlist
status: New → Incomplete
Dustin Kirkland  (kirkland) wrote :

Hi Ryan,

Thanks for the report. I'd like to bind this to ctrl-F5 command, which calls byobu-reconnect-sockets, and already handles gpg, ssh, and dbus sessions. It would be nice if it did X too.

I've struggled with this in the past and have never gotten it working entirely. Can you propose a patch or branch for merging?

Ryan Thompson (rct86) wrote :

If I'm reading it correctly, it looks like the implementation of byobu-reconnect-sockets only works at the shell level. So it would only fix the environment variables in the current shell, and would need to be re-run in every existing shell and every newly-created shell window as well.

The script that I wrote actually updates the terminal multiplexer's (screen's or tmux's) values for the environment variables in question, so that any newly-spawned shell windows inside the multiplexer will inherit the new value. Obviously, existing windows are unaffected, but one could write a shell function to query the multiplexer for the required environment variables and set them in the shell.

Basically, I'm thinking that what is required are a pair of scripts "byobu-sendenv" and "byobu-getenv". The first would be used outside of the multiplexer like:

$ byobu-sendenv DISPLAY

and would send the current value of DISPLAY to byobu's multiplexer. The second would be used inside byobu in a running shell like:

$ DISPLAY=$(byobu-getenv DISPLAY)

will send any environment variable you tell it to send to the session of your choice. Once those two support scripts are in place, the byobu launch process would go something like this:

1. Check if byobu is running. If not, start it in the background, and don't connect to it yet.
2. byobu-sendenv DISPLAY SESSION_MANAGER DBUS_SESSION_BUS_ADDRESS ...
3. Connect to the session, which now has the correct environment variables set
4. Define a shell alias or keyboard shortcut to propogate the new values of these variables to the shell in the current window

The rest is simply knowing which environment variables to send. For the X11 session, obviously you need to send DISPLAY, but there may be others as well that I don't know about. You would probably have a config file somewhere telling byobu which environment variables should be handled in this way, which defaults to SESSION_MANAGER, DBUS_SESSION_BUS_ADDRESS, DISPLAY, etc.

Anyway, feel free to use my script for testing purposes, but it probably has too many dependencies to actually include in byobu. That's why you can use the --verbose option to have it tell you what it's doing.

I might get around to implementing it myself eventually, but it's not high on my list of priorities.

Ryan Thompson (rct86) wrote :

I have written a basic solution. There are two parts. First, I modified byobu-select-session so that before it attaches to an existing session, it sends the values of "DISPLAY", "DBUS_SESSION_BUS_ADDRESS", "SESSION_MANAGER", and "GPG_AGENT_INFO" from the current shell to screen/tmux, and *then* attaches to it. From then on, any new windows created in screen/tmux will have the new values for those environment variables.

Second, I wrote a script called "byobu-update-environment" that you source from your shell inside byobu. It queries screen/tmux for those same environment variables and exports them in the current shell. This is for the shells that you already had running before you attached and updated the environment.

As soon as I figure out how to do stuff with bzr, I'll publish my changes somewhere you can look at them. bot for tonight, it's bedtime.?field.comment=I have written a basic solution. There are two parts. First, I modified byobu-select-session so that before it attaches to an existing session, it sends the values of "DISPLAY", "DBUS_SESSION_BUS_ADDRESS", "SESSION_MANAGER", and "GPG_AGENT_INFO" from the current shell to screen/tmux, and *then* attaches to it. From then on, any new windows created in screen/tmux will have the new values for those environment variables.

Second, I wrote a script called "byobu-update-environment" that you source from your shell inside byobu. It queries screen/tmux for those same environment variables and exports them in the current shell. This is for the shells that you already had running before you attached and updated the environment.

It basically works, but it could probably be made better before releasing it, and it needs testing.

As soon as I figure out how to do stuff with bzr, I'll publish my changes the right way (I mostly use git). but for now, it's bedtime, so I'm just going to attach a patch here that you can try it out.

TODO: Make byobu-reconnect-sockets also send environment variables to screen/tmux so that new shells get the new values.

Ryan Thompson (rct86) wrote :

Whoa. Something weird happened with that message. Let me try that again:

I have written a basic solution. There are two parts. First, I modified byobu-select-session so that before it attaches to an existing session, it sends the values of "DISPLAY", "DBUS_SESSION_BUS_ADDRESS", "SESSION_MANAGER", and "GPG_AGENT_INFO" from the current shell to screen/tmux, and *then* attaches to it. From then on, any new windows created in screen/tmux will have the new values for those environment variables.

Second, I wrote a script called "byobu-update-environment" that you source from your shell inside byobu. It queries screen/tmux for those same environment variables and exports them in the current shell. This is for the shells that you already had running before you attached and updated the environment.

It basically works, but it could probably be made better before releasing it, and it needs testing.

As soon as I figure out how to do stuff with bzr, I'll publish my changes the right way (I mostly use git). but for now, it's bedtime, so I'm just going to attach a patch here that you can try it out.

TODO: Make byobu-reconnect-sockets also send environment variables to screen/tmux so that new shells get the new values.

Ryan Thompson (rct86) wrote :

Here is the patch for byobu-reconnect-sockets. With this, in addition to updating the variables in the current shell, it also sends the variables to screen/tmux

Dustin Kirkland  (kirkland) wrote :

Moving from Incomplete to Triaged.

Thanks a lot, Ryan, this is looking really good! I'll do some testing of it now. I like the approach.

Changed in byobu:
status: Incomplete → Triaged
Changed in byobu:
status: Triaged → In Progress
Dustin Kirkland  (kirkland) wrote :

Okay, I've committed a modified version of these. I've imported the functionality you had in byobu-update-environment into byobu-reconnect-sockets. The functionality was quite similar, and it would be useful to get all of this benefit from pressing ctrl-f5 in an existing window. Your new code helps ensure that these are set correctly on newly created windows. Thanks!

Changed in byobu:
status: In Progress → Fix Committed
Ryan Thompson (rct86) wrote :

So, now that this is working, are there any other variables that we need to add to the list? Here are some candidates from looking at my environment:

$ env | grep SSH"

# env | grep KONSOLE # When running inside konsole

$ env | grep GNOME_KEYRING

GDMSESSION, XDG_CURRENT_DESKTOP, XDG_SEAT_PATH, XDG_SESSION_COOKIE, WINDOWID?

I have no idea which, if any, of these should actually be updated upon attaching, but probably some of them should.

Changed in byobu:
status: Fix Committed → Fix Released

Sorry, I don't know either... Might need to talk to some of the X
guys in #ubuntu-desktop? Perhaps Pitti?

Ryan Thompson (rct86) wrote :

I've just been using the new version, and I realized that if you only have a single session, byobu-select-session doesn't call update_environment before attaching to it. This is because the attachment code is somewhat duplicated, and I only fixed one instance. I'm attaching a patch to fix this by eliminating the code duplication, causing all attachment to existing sessions to happen via the attach_session function.

Changed in byobu:
status: Fix Released → Triaged
status: Triaged → Fix Committed
Changed in byobu:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers