【发布时间】: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/…