xfconf-query,crontab,“无法初始化libxfconf:无法为X11自动启动没有$ DISPLAY的dbus-daemon。”

我有Xubuntu 16.04,我正在尝试从crontab运行以下脚本:

#!/bin/bash status=$(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac) vid="/dev/video0" if [ -z "$status" ]; then exit 1 fi if [ -e "$vid" -a "$status" -gt 14 ]; then xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac -s 14 elif [ ! -e "$vid" -a "$status" -eq 14 ]; then xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac -s 25 fi 

从终端运行它时效果很好。 但是,从crontab我得到这个错误。

 Failed to init libxfconf: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11. 

这是我的crontab条目。 它是使用crontab -e编辑的。

 */5 * * * * (bash -x /home/brock/bin/vid-power) > /home/brock/Desktop/debug.log 2>&1 

这是我的debug.log的完整输出。

 ~/Desktop$ cat debug.log ++ xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac Failed to init libxfconf: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11. + status= + vid=/dev/video0 + '[' -z '' ']' + exit 1 

我尝试了各种解决方案,包括这里的评论和这个 ,但没有一个有效。

我做了以下,它允许我从crontab调用xfconf-query

首先,获取此变量的值:

 echo $DBUS_SESSION_BUS_ADDRESS 

你会看到这样的路径:

 unix:path=/run/user/1000/bus 

然后使用:

 env DBUS_SESSION_BUS_ADDRESS=[path] xfconf-query .... 

我不明白它背后的详细机制,但它对我有用:)

我将设置此脚本作为Session和Startup> Application Autostart项目运行。

 #!/bin/bash vid="/dev/video0" while true; do status=$(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac) if [ -e "$vid" -a "$status" -gt 14 ]; then xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac -s 14 elif [ ! -e "$vid" -a "$status" -eq 14 ]; then xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac -s 25 fi sleep 5m done