在初始化脚本中启动时如何设置会话 DBus 的环境变量?

How to set the enviroment variables of the session DBus when starting in an init script?

在启动我的应用程序守护程序之前,我想确保会话模式下的 DBus 是 运行。 /etc/init.d/ 下的 sh 脚本看起来像:

#!/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 "" in
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       status
       ;;
    *)
       echo "Usage: [=10=] {start|stop|status|restart}"
esac

exit 0

脚本运行良好。但是全局环境变量(DBUS_SESSION_BUS_PID、DBUS_SESSION_BUS_ADDRESS)并不是全局设置的。我发现服务有一些 constrains regarding environment variables.

我的脚本是通过 System V 初始化系统执行的。当 运行 它在控制台时,环境变量也没有设置。

有解决该问题的想法吗?

这个问题可以通过直接为进程设置环境变量来解决:

#!/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 "" in
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       status
       ;;
    *)
       echo "Usage: [=10=] {start|stop|status|restart}"

与其使用 dbus-launch 启动 dbus 然后尝试将您的应用程序连接到它,不如尝试 运行 您的应用程序 dbus-run-session。 (参见 https://dbus.freedesktop.org/doc/dbus-run-session.1.html and https://dbus.freedesktop.org/doc/dbus-launch.1.html