4

Yes, I know that this question is a bit confusing, I just mean "the variables which are acessible from session's command line".

I am writing an application which runs automatically by upstart on filesystem, to listen to DBus login1 events and thus It runs from root. But after any user logs in I need to listen to his session bus for additional signals. That's why I want to get DBUS_SESSION_BUS_ADDRESS of current user.

Other methods of getting DBUS_SESSION_BUS_ADDRESS don't work for me, for example ~/.dbus/session-bus/* files are somehow outdated(?) ie their content does not match one in the environment.

  • if you know of a process id for the user, you can look in /proc/<pid>/environ for all the env var=value (each terminated by null). – meuh Oct 08 '15 at 16:43

2 Answers2

3

It sounds like you are trying to find the variable that a user has in their environment dynamically from an already running script as each process gets created?

A method I have used to solve this is to find the PID, then use the PID to parse that processes environment variables from /proc/PID/environ. This only works if the PID inherits the variables.

pid=1234         # replace with pid finding method
unset thisbus    # in case this snippet gets pasted into a loop
[ -f /proc/$pid/environ ] && thisbus="$(sed 's/^.*\x0DBUS_SESSION_BUS_ADDRESS=\([^\x0]*\)\x0.*$/\1/' /proc/$pid/environ)"
[ -z "$thisbus" ] && echo "not found" || echo "$thisbus"
Mel
  • 610
0

Besides the obvious echo

[sheepdog@dogpoung ~]$ echo $DBUS_SESSION_BUS_ADDRESS
unix:abstract=/tmp/dbus-cdhvJBSPVi,guid=5451e8f0f49db972ccc8f845560efd7e

In bash the export action shows you all(?) of the variables.

export - Names of exported shell variables. May also be specified as -e.

[sheepdog@dogpoung ~]$ export
declare -x COLORTERM="gnome-terminal"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-cdhvJBSPVi,guid=5451e8f0f49db972ccc8f845560efd7e"
declare -x DESKTOP_SESSION="gnome-classic"
declare -x DISPLAY=":0"
declare -x GDMSESSION="gnome-classic"
declare -x GDM_LANG="en_US.utf8"
declare -x GJS_DEBUG_OUTPUT="stderr"
...
0xSheepdog
  • 2,750
  • Why this answer was given a -1? Just curious; personal growth and all that. – 0xSheepdog Oct 08 '15 at 20:37
  • 1
    I think what the OP wants is to get the environment variables form another session (could be another user but also same user, with another X session running). This answer only prints out the environment variables in the current session. – Luciano Sep 08 '18 at 12:55