【问题标题】:How to set the enviroment variables of the session DBus when starting in an init script?在初始化脚本中启动时如何设置会话 DBus 的环境变量?
【发布时间】:2016-11-25 02:34:24
【问题描述】:

我喜欢在启动我的应用程序守护程序之前确保会话模式下的 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 "$1" in
    start)
       start
       ;;
    stop)
       stop
       ;;
    restart)
       stop
       start
       ;;
    status)
       status
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
esac

exit 0

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

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

有什么办法可以解决这个问题吗?

【问题讨论】:

  • 还是什么都没有?也许将其移植到 systemd...
  • 我正在使用用户构建 Linux(Yocto)。抱歉,只有 System V 可用。
  • 似乎在登录时这些环境变量丢失了来源:unix.stackexchange.com/questions/26150/…

标签: linux bash dbus init.d


【解决方案1】:

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

#!/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}"

【讨论】:

  • 你能解释一下吗?
  • 一段时间过去了。环境变量归用户所有。这就是为什么 dbus pid 和地址存储在 /var/run/ 的一些文件中的原因。这些文件夹由所有用户共享。发生什么了?如果 dbus 的 pid 已知,则此脚本会查找。如果不是,它会启动 dbus 并将其 pid 保存到 var/run/sessionbus.pid。在检查应用程序是否正在运行之后。如果没有,则为应用进程设置 dbus 地址。
猜你喜欢
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
  • 2020-05-03
  • 2021-12-10
  • 2012-01-12
  • 1970-01-01
  • 2011-11-26
相关资源
最近更新 更多