How to export DBUS_SESSION_BUS_ADDRESS
I've finally found the answer, running the following command exports the output of dbus-launch:
export $(dbus-launch)
Shell script with export command and notify-send via crontab not working. Exported variable is set by a command
env
when invoked from a script run by cron
, only had the following variables set in my box.
SHELL=/bin/sh USER=clement PATH=/usr/bin:/bin PWD=/home/clement HOME=/home/clement SHLVL=2 LOGNAME=clement _=/usr/bin/env
and so grep
in
$(env | grep DBUS_SESSION_BUS_ADDRESS | sed 's/DBUS_SESSION_BUS_ADDRESS=//')
didn't give any output. The following should work.
#!/bin/bash
PID=$(pgrep gnome-session)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
export DBUS_SESSION_BUS_ADDRESS=$dbus
notify-send -u critical "Blah" "Blubb"
Python DBUS SESSION_BUS - X11 dependency
The problem is that you're running the export
calls in separate shells. You need to capture the output of dbus-launch
, parse the values, and use os.environ
to write them to the environment:
p = subprocess.Popen('dbus-launch', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for var in p.stdout:
sp = var.split('=', 1)
print sp
os.environ[sp[0]] = sp[1][:-1]
awk output to export variable
Solved.
export DBUS_SESSION_BUS_ADDRESS=$(dbus-daemon --session --fork --print-address | awk -F"dbus-" '{ print $2 }' | awk -F",guid=" '{print ("unix:abstract=/tmp/dbus-" $1 ",guid=" $2)}')
For future reference
How to set the enviroment variables of the session DBus when starting in an init script?
This problem can be overcome by setting the enviroment variables direct for the process:
#!/bin/sh
### BEGIN INIT INFO
# Provides: myApp
# Required-Start: $local_fs $syslog mountkernfs
# Required-Stop: $local_fs $syslog mountkernfs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts myApp
### END INIT INFO
# Source function library.
. /etc/init.d/functions
start() {
echo Starting myApp Deamon
if [ -e "/var/run/dbus/sessionbus.pid" ];then
DBUS_SESSION_BUS_PID=`cat /var/run/dbus/sessionbus.pid`
fi
if [ -e "/var/run/dbus/sessionbus.address" ];then
DBUS_SESSION_BUS_ADDRESS=`cat /var/run/dbus/sessionbus.address`
fi
# start the dbus as session bus and save the enviroment vars
if [ -z ${DBUS_SESSION_BUS_PID+x} ];then
echo start session dbus ...
eval "export $(/usr/bin/dbus-launch)"
echo "${DBUS_SESSION_BUS_PID}">/var/run/dbus/sessionbus.pid
echo "${DBUS_SESSION_BUS_ADDRESS}">/var/run/dbus/sessionbus.address
echo session dbus runs now at pid="${DBUS_SESSION_BUS_PID}"
else
echo session dbus runs at pid="${DBUS_SESSION_BUS_PID}"
fi
pgrep -x myApp
if [ "$?" = "0" ]
then
echo myApp Deamon already running
exit 1
fi
DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS} myApp -d
echo myApp started successfully
}
stop() {
echo Stopping myApp Deamon
kill -15 `pgrep myApp`
}
status() {
pgrep myApp > /dev/null && echo running
pgrep myApp > /dev/null || echo stopped
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
Related Topics
How to Set a Variable Used in a Perl Script as Environment Variable
Perl Fails to Set Locale Even Though It Is Installed
Merge Files with Bash by Primary Key
Matlab Mex Socket Wrapper Library
Connect to Host Localhost Port 22: Connection Refused
Copy Files from Windows to Windows Subsystem for Linux (Wsl)
Docker Networking Namespace Not Visible in Ip Netns List
Crontab Is Not Working on Amazon Ec2 Server
Cuda Compiler Not Working with Gcc 4.5 +
Bash Echo with an $ Character Outside the String
Modify Http Headers on System Level (Linux)
Is There a Command to List All Unix Group Names
Linux, Where Are the Return Codes Stored of System Daemons and Other Processes
Stty Serial Port Settings for Parity Not Persistent
Grep with Continuous Pipe Does Not Work
Opening a .Tar.Gz File with a Single Command
How to Execute an Local Script in Remote Server with Parameters