DBUS-LAUNCH(1)

πŸ’– DBUS-LAUNCH(1) πŸ’–

okay bestie, gather round, because we're talking about dbus-launch β€” a lil utility from the User Commands corner of your manual pages, section 1, that starts a message bus from a shell script. she's giving little-helper-that-could energy and honestly? obsessed. πŸ¦‹


✨ NAME ✨

dbus-launch β€” utility to start a message bus from a shell script πŸ’…


✨ SYNOPSIS ✨

dbus-launch [--version] [--help] [--sh-syntax] [--csh-syntax]
            [--auto-syntax] [--binary-syntax] [--close-stderr]
            [--exit-with-session] [--exit-with-x11]
            [--autolaunch=MACHINEID] [--config-file=FILENAME] [PROGRAM]
            [ARGS...]

✨ DESCRIPTION ✨

so the way that dbus-launch works is: it starts up a session bus instance of dbus-daemon from inside a shell script. she's normally called from a user's login scripts, like, that's her whole job. and here's the thing that makes her special β€” unlike the daemon itself, dbus-launch exits. so you can use backticks or the $() construct to read info back out of her. iconic behaviour, honestly. ✨

with no arguments at all? dbus-launch will launch a session bus instance and print the address and PID of that instance straight to standard output. slay.

you can also specify a program to be run, and in that case dbus-launch will launch a session bus instance, set the appropriate environment variables so your specified program can find the bus, and then execute that program with the arguments you gave it. examples are down below, don't worry 🫢

heads up bestie: if you launch a program, dbus-launch will not print the info about the new bus to standard output. no bus gossip for you in that case.

when dbus-launch does print bus info to standard output, by default it comes out in a simple key-value pairs format. but you can request several alternate syntaxes with the --sh-syntax, --csh-syntax, --binary-syntax, or --auto-syntax options. several of these make dbus-launch emit shell code to set up the environment β€” she's literally writing code for you, main character energy.

with the --auto-syntax option, dbus-launch looks at the value of the SHELL environment variable to figure out which shell syntax to use. if SHELL ends in "csh", then csh-compatible code gets emitted; otherwise it's Bourne shell code. instead of passing --auto-syntax, you may explicitly specify a particular one by using --sh-syntax for Bourne syntax, or --csh-syntax for csh syntax. and fr fr, in scripts it's more robust to avoid --auto-syntax β€” you hopefully know which shell your script is written in πŸ’€

more info about D-Bus lives at https://www.freedesktop.org/wiki/Software/dbus/ and you should also peep the man page for dbus-daemon. iykyk.


🌸 EXAMPLES 🌸

distributions that run dbus-launch as part of a standard X session should run dbus-launch --exit-with-session after the X server has started and become available, as a wrapper around the "main" X client (typically a session manager or window manager). like these:

dbus-launch --exit-with-session gnome-session
dbus-launch --exit-with-session openbox
dbus-launch --exit-with-session ~/.xsession

if your distribution does not do this, you can achieve similar results by running your session or window manager in the same way in a script run by your X session, such as ~/.xsession, ~/.xinitrc or ~/.Xclients. we love a DIY moment πŸ’…

to start a D-Bus session within a text-mode session, do not use dbus-launch. instead, see dbus-run-session(1). periodt.

## test for an existing bus daemon, just to be safe
if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
    ## if not found, launch a new one
    eval `dbus-launch --sh-syntax`
    echo "D-Bus per-session daemon address is: $DBUS_SESSION_BUS_ADDRESS"
fi

note that in this case, dbus-launch will exit, and dbus-daemon will not be terminated automatically on logout. 😭 that daemon is staying, she lives here now.


🌈 AUTOMATIC LAUNCHING 🌈

if DBUS_SESSION_BUS_ADDRESS is not set for a process that tries to use D-Bus, then by default that process will attempt to invoke dbus-launch with the --autolaunch option, to either start up a new session bus or find the existing bus address on the X display or in a file in ~/.dbus/session-bus/.

whenever an autolaunch happens, the application that had to start a new bus ends up in its own little world. like, she can effectively end up starting a whole new session if she tries to use a lot of bus services. this can be suboptimal or even totally broken, depending on the app and what it's trying to do. πŸ’€

there are two common reasons for autolaunch:

  1. ssh to a remote machine. the ideal fix for that would be forwarding of DBUS_SESSION_BUS_ADDRESS in the same way that DISPLAY is forwarded. in the meantime, you can edit the session.conf config file to have your session bus listen on TCP, and manually set DBUS_SESSION_BUS_ADDRESS, if you like.
  2. an su to another user, and display of X applications running as the second user on the display belonging to the first user. perhaps the ideal fix in this case would be to allow the second user to connect to the session bus of the first user, just as they can connect to the first user's display. however, a mechanism for that has not been coded. πŸ™ˆ

you can always avoid autolaunch by manually setting DBUS_SESSION_BUS_ADDRESS. autolaunch happens because the default address if none is set is "autolaunch:" β€” so if any other address is set, there will be no autolaunch. you can however include autolaunch in an explicit session bus address as a fallback, for example DBUS_SESSION_BUS_ADDRESS="something:,autolaunch:" β€” in that case, if the first address doesn't work, processes will autolaunch. (the bus address variable contains a comma-separated list of addresses to try, bestie.)

the --autolaunch option is considered an internal implementation detail of libdbus, and in fact there are plans to change it. there's no real reason to use it outside of the libdbus implementation anyhow. so like, hands off unless you're libdbus. πŸ”₯


πŸ’… OPTIONS πŸ’…

the following options are supported:

  • --auto-syntax β€” choose --csh-syntax or --sh-syntax based on the SHELL environment variable.
  • --binary-syntax β€” write to stdout a nul-terminated bus address, then the bus PID as a binary integer of size sizeof(pid_t), then the bus X window ID as a binary integer of size sizeof(long). integers are in the machine's byte order, not network byte order or any other canonical byte order.
  • --close-stderr β€” close the standard error output stream before starting the D-Bus daemon. this is useful if you want to capture dbus-launch error messages but you don't want dbus-daemon to keep the stream open to your application.
  • --config-file=FILENAME β€” pass --config-file=FILENAME to the bus daemon, instead of passing it the --session argument. see the man page for dbus-daemon.
  • --csh-syntax β€” emit csh compatible code to set up environment variables.
  • --exit-with-x11 β€” if this option is provided, a persistent "babysitter" process will be created, and will connect to the X server. if it cannot do so, launching fails. if the "babysitter" process loses its X connection, it kills the message bus daemon, disconnecting all of its clients (which should exit in response). this avoids having leftover daemon processes from a user X session, after the X session has ended. 🧚
  • --exit-with-session β€” if this option is provided, a persistent "babysitter" process will be created, as if for --exit-with-x11. if it cannot connect to the X server, it will monitor the terminal from which dbus-launch was started instead, and if it gets a HUP on stdin, the message bus daemon will be killed. this option is not recommended, since it will consume input from the terminal where it was started; it is mainly provided for backwards compatibility.
  • --autolaunch=MACHINEID β€” this option implies that dbus-launch should scan for a previously-started session and reuse the values found there. if no session is found, it will start a new session. the --exit-with-session option is implied if --autolaunch is given. this option is for the exclusive use of libdbus, you do not want to use it manually. it may change in the future.
  • --sh-syntax β€” emit Bourne-shell compatible code to set up environment variables.
  • --version β€” print the version of dbus-launch.
  • --help β€” print the help info of dbus-launch.

⭐ NOTES ⭐

if you run dbus-launch myapp (with any other options), dbus-daemon will not exit when myapp terminates. the reason is that myapp is assumed to be part of a larger session, rather than a session in its own right. so she's a team player, not a solo act πŸ’œ


πŸ’– AUTHOR πŸ’–

see https://dbus.freedesktop.org/doc/AUTHORS


πŸ¦‹ BUGS πŸ¦‹

please send bug reports to the D-Bus mailing list or bug tracker, see https://www.freedesktop.org/wiki/Software/dbus/


and that's the whole page bestie πŸŽ€ this was D-Bus 1.16.2, manual section DBUS-LAUNCH(1) β€” go forth, launch your bus, and stay hydrated βœ¨πŸ’–

πŸ“¦ where this page comes from