在ubuntu16.04下写了一个设置壁纸的脚本,终端下运行正常,放到crontab里面运行报错,报错如下:
(process:9896): dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY
(process:9899): dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY
是环境变量的问题,搜了一下,找到一种可行的办法:
在脚本前面添加如下代码:
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
例如我的代码如下:
#!/bin/bash
# 这种方法是可行的
# export DBUS_SESSION_BUS_ADDRESS environment variable
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
# 设置壁纸
/home/leon/set_wallpape.py