添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.

Sign up to join this community

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

xfconf-query, crontab, "Failed to init libxfconf: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11."

Ask Question
#!/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
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

It works perfectly when run it from the terminal. However, from crontab I get this error.

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

Here is my crontab entry. It was edited using crontab -e.

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

Here is the full output of my 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

I have tried various solutions, including the comment here and this one, but none have worked.

I did the following, it allows me to invoke xfconf-query from crontab:

First, get the value of this variable:

echo $DBUS_SESSION_BUS_ADDRESS

You will see a path like this:

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

Then use, in the script executed by crontab:

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus
xfconf-query ...

Credit: https://bbs.archlinux.org/viewtopic.php?pid=1706208#p1706208

I will just set this script to run as a Session and Startup > Application Autostart item instead.

#!/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
    sleep 5m

In order for the xfconf-query command to work under cron environment you need to have the following in the bash script, for example:

# set today's wallpaper; first set one env variable for xfconf-query
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitorVGA-0/workspace0/last-image -s "${filename}"

Credit: https://bbs.archlinux.org/viewtopic.php?pid=1706208#p1706208