How to export DBUS_SESSION_BUS_ADDRESS

https://stackoverflow.com/questions/41242460/how-to-export-dbus-session-bus-address

Asked 9 years ago Modified 3 years, 3 months ago Viewed 75k times 19

I’m trying to run D-Bus on an embedded system (Yocto Linux) and connect to it from my application code.

I get the following error when I call dbus_bus_get(DBUS_BUS_SESSION, &err);

Using X11 for dbus-daemon autolaunch was disabled at compile time, set your DBUS_SESSION_BUS_ADDRESS instead I realise that I need to start the dbus-daemon first so I have run dbus-launch from the command line.

This prints out a value of DBUS_SESSION_BUS_ADDRESS but how could I export it programmatically?

linuxdbusyocto Share Improve this question Follow edited Oct 12, 2021 at 18:10 Aaron Hall’s user avatar Aaron Hall♦ 400k9393 gold badges417417 silver badges342342 bronze badges asked Dec 20, 2016 at 12:32 User55412’s user avatar User55412 95022 gold badges1010 silver badges3232 bronze badges Add a comment 6 Answers Sorted by:

Highest score (default) 41

I’ve finally found the answer, running the following command exports the output of dbus-launch:

export $(dbus-launch) Share Improve this answer Follow edited Mar 29, 2017 at 7:08 answered Dec 20, 2016 at 14:09 User55412’s user avatar User55412 95022 gold badges1010 silver badges3232 bronze badges Sign up to request clarification or add additional context in comments.

6 Comments

Aaron Miller Over a year ago This starts a new dbus-daemon process. Is that desirable?

fstamour Over a year ago I probably is in a “embedded” system, where you have to explicitly start everything in order to preserve resources.

David L. Over a year ago This seems to be X11 specific, does not work on Wayland.

An5Drama Over a year ago @DavidL. You can try export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus. It works for me. (I tried in cron, it seems to not know $XDG_RUNTIME_DIR).

An5Drama Over a year ago IMHO, it is better to use the currently running dbus’s DBUS_SESSION_BUS_ADDRESS. See this QA answer for how to find that. Add a comment | Show 1 more comment 11

pid_gnome=$(pgrep gnome-session) DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/${pid_gnome}/environ|cut -d= -f2-) export DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS} Please make sure that the user has the DISPLAY variable set.

Another alternative is:

export $(dbus-launch) Share Improve this answer Follow edited Oct 7, 2019 at 0:46 eyllanesc’s user avatar eyllanesc 246k1919 gold badges205205 silver badges282282 bronze badges answered Feb 12, 2019 at 23:54 user3801989’s user avatar user3801989 11111 silver badge22 bronze badges 2 Comments

wataash Over a year ago pgrep gnome-session may output multiple PIDs (e.g. three processes on Ubuntu 20.04), so it should be pgrep gnome-session | head -1. DBUS_SESSION_BUS_ADDRESS=$(grep … | cut -d= -f2-) ends with 000a (\0\n), which causes warning bash: warning: command substitution: ignored null byte in input when export. So it should be DBUS_SESSION_BUS_ADDRESS=$(grep … | cut -d= -f2- | tr -d ‘\0\n’).

wataash Over a year ago WIth grep -P, | cut is not needed. export DBUS_SESSION_BUS_ADDRESS=$(grep -P -o -a “(?<=DBUS_SESSION_BUS_ADDRESS=).+?(?=\x00)” /proc/”$(pgrep gnome-session | head -1)”/environ) 8

Type the following command into a terminal:

eval dbus-launch --auto-syntax Share Improve this answer Follow edited Aug 13, 2020 at 7:53 User55412’s user avatar User55412 95022 gold badges1010 silver badges3232 bronze badges answered Mar 26, 2019 at 8:22 Mostafa Aghashahi’s user avatar Mostafa Aghashahi 8911 silver badge33 bronze badges 3 Comments

Dwhitz Over a year ago Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

Jonathan Komar Over a year ago Please fix your backslash. I cannot because it would not be enough characters to allow a change.

haofeng Over a year ago This is not good ans. After doing this, I cant use RDP. Add a comment 2

When you launch your user session, do it like this:

dbus-daemon –session –fork –print-address 1 > /tmp/dbus-session-addr.txt This will cause the session address to be written to /tmp/dbus-session-addr.txt. (The filename’s not that important, it’s just somewhere you’ve decided to store it.)

Then, when you need the variable set:

export DBUS_SESSION_BUS_ADDRESS=$(cat /tmp/dbus-session-addr.txt) If your shell’s sniffy about export-during-definition - some can be - do it in two stages:

DBUS_SESSION_BUS_ADDRESS=$(cat /tmp/dbus-session-addr.txt) export DBUS_SESSION_BUS_ADDRESS Share Improve this answer Follow answered Sep 1, 2022 at 12:11 JonGreen’s user avatar JonGreen 37911 silver badge55 bronze badges Comments

1

It sounds like you are trying to get the value of DBUS_SESSION_BUS_ADDRESS in order for your application to run correctly. Try running it with dbus-run-session instead of dbus-launch. According to https://dbus.freedesktop.org/doc/dbus-launch.1.html dbus-launch should not be run from the command line, but in a shell script (see also https://dbus.freedesktop.org/doc/dbus-run-session.1.html).

Share Improve this answer Follow answered May 19, 2022 at 15:37 ernobe’s user avatar ernobe 3122 bronze badges Comments

-7

Type in terminal:

export $DBUS_SESSION_BUS_ADDRESS Share Improve this answer Follow edited Dec 3, 2017 at 9:33 Jamal’s user avatar Jamal 77177 gold badges2222 silver badges3232 bronze badges answered Dec 1, 2017 at 22:13 Syfi Malik’s user avatar Syfi Malik 755 bronze badges 2 Comments

calexandru Over a year ago If the variable is not set, exporting it will not solve anything, thus $(dbus-launch) solves this issue.

Arnaud Meuret Over a year ago This is worse than wrong as it will try to export a variable named after the contents of DBUS_SESSION_BUS_ADDRESS which makes no sense.

Updated: