1

I like to make sure that the DBus in session mode is running before starting my Application deamon. The sh script under /etc/init.d/ looks like:

#!/bin/sh

### BEGIN INIT INFO
# Provides:           myAppD
# 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

        # start the dbus as session bus and save the enviroment vars
        if [ -z ${DBUS_SESSION_BUS_PID+x} ]; then
                echo start session dbus ...
                eval $(/usr/bin/dbus-launch --sh-syntax)
                echo session dbus runs now at pid=${DBUS_SESSION_BUS_PID}
        else
                echo session dbus runs at pid=${DBUS_SESSION_BUS_PID}
        fi

    pgrep myApp
    if [ "$?" = "0" ]
    then
        echo myApp already running
        exit 0
    fi
    myApp
    echo myApp started successfully
}

stop() {
    echo Stopping myApp
    kill -SIGHUP `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}"
esac

exit 0

The script works fine. But the global environment variables(DBUS_SESSION_BUS_PID, DBUS_SESSION_BUS_ADDRESS) are not set globally. I found out that service has some constrains regarding environment variables.

My script is executed via the System V init system. When running it at the console the environment variables are not set as well.

Any ideas how to solve that problem?

Community
  • 1
  • 1
Stefan Jaritz
  • 1,999
  • 7
  • 36
  • 60

2 Answers2

1

Instead of launching dbus with dbus-launch and then trying to connect your application to it, try running your application with dbus-run-session. (See https://dbus.freedesktop.org/doc/dbus-run-session.1.html and https://dbus.freedesktop.org/doc/dbus-launch.1.html)

ernobe
  • 11
  • 1
0

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}"
Stefan Jaritz
  • 1,999
  • 7
  • 36
  • 60
  • Can you provide some explanation? – Jake Sep 26 '20 at 17:04
  • a bit of time has passed. Environmental variables are owned by the users. That is why the dbus pid and address are stored inside some files at /var/run/. These folder is shared through all users. What happens? This script looks up if the pid of the dbus is known. If not it starts the dbus and saves its pid to var/run/sessionbus.pid. After it is checked if the app is running. If not the dbus address is set for the app process. – Stefan Jaritz Sep 30 '20 at 15:46