How to prevent ssh -X from hanging on exit when dbus is used

Programs like virt-manager and gedit commonly use dbus. Dbus running will prevent a clean exit of ssh -X, making it hang on exit until you press ctrl+c or kill dbus manually.
To avoid having to kill dbus manually, let’s kill it automatically 🙂

Now, normally if I want to run virt-manager (or anything else graphical) on a remote machine with X forwarding, I’d use a command like

ssh -X root@remote-server virt-manager

Instead, I want to use dbus’ ability to output sh-compatible commands to set invironment variables and kill the PID I get from that before exiting, like so:

ssh -X root@remote-server 'eval $(dbus-launch --close-stderr --sh-syntax); virt-manager --no-fork; kill -TERM $DBUS_SESSION_BUS_PID'

Problem solved!  The remote shell (bash) now starts dbus manually instead of letting virt-manager do it, waits for virt-manager to exit (–no-fork), then kills dbus and exits.

Leave a Reply

Your email address will not be published.