可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I wrote a bash script that changes the wallpaper (for GNOME3).
#!/bin/bash # Wallpaper's directory. dir="${HOME}/images/wallpapers/" # Random wallpaper. wallpaper=`find "${dir}" -type f | shuf -n1` # Change wallpaper. # http://bit.ly/HYEU9H gsettings set org.gnome.desktop.background picture-options "spanned" gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}"
Script executed in a terminal emulator (eg gnome-terminal) works great. During the execution by cron, or ttyX terminal getting the error:
** (process:26717): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n ** (process:26717): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n ** (process:26721): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n ** (process:26721): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n
回答1:
Finally I managed how to solve this issue after many, many attempts.
Indeed, the problem occur because cron uses only a very restricted set of environment variables. And the only one environment variable that is responsible for running in the right way the script from the question when this is set as a cron job is DBUS_SESSION_BUS_ADDRESS
, not DISPLAY
or XAUTHORITY
or GSETTINGS_BACKEND
or something else. This fact was also pointed well in this answer.
But the problem in this answer is that there's no guarantee that the DBUS_SESSION_BUS_ADDRESS
variable from that file from ~/.dbus/session-bus/
directory is updated to the current value from the current gnome session. To go over this problem a method would be to find the PID of a process in the current gnome session, and obtain the dbus address from its environment. We can do this as follow:
PID=$(pgrep gnome-session) # instead of 'gnome-session' it can be also used 'noutilus' or 'compiz' or the name of a process of a graphical program about that you are sure that is running after you log in the X session export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
That being said, the script should look like:
#!/bin/bash # TODO: At night only dark wallpapers. # Wallpaper's directory. dir="${HOME}/images/wallpapers/" # 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-) # Random wallpaper. wallpaper=`find "${dir}" -type f | shuf -n1` # Change wallpaper. # http://bit.ly/HYEU9H gsettings set org.gnome.desktop.background picture-options "spanned" gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}"
回答2:
I found some solutions. When you export a variable DBUS_SESSION_BUS_ADDRESS contained in the file ~/.dbus/session-bus/*, dbus-launch does not tell more about the error. However, instead of wallpaper there are artefacts.
Added code:
sessionfile=`find "${HOME}/.dbus/session-bus/" -type f` export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'`
Now the script looks like this:
#!/bin/bash # TODO: At night only dark wallpapers. # Wallpaper's directory. dir="${HOME}/images/wallpapers/" # Weird, but necessary thing to run with cron. sessionfile=`find "${HOME}/.dbus/session-bus/" -type f` export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'` # Random wallpaper. wallpaper=`find "${dir}" -type f | shuf -n1` # Change wallpaper. # https://superuser.com/questions/298050/periodically-changing-wallpaper-under-gnome-3/298182#298182 gsettings set org.gnome.desktop.background picture-options "spanned" gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}"
回答3:
Tried this and it worked great for me:
dbus-launch --exit-with-session gsettings set schema key value
Or from root cron:
sudo -u user dbus-launch --exit-with-session gsettings set schema key value
Credit: http://php.mandelson.org/wp2/?p=565
回答4:
To change your wallpaper through cron, just do this directly in your crontab : Execute crontab -e
Add lines like this :
30 09 * * * DISPLAY=:0 GSETTINGS_BACKEND=dconf /usr/bin/gsettings set org.gnome.desktop.background picture-uri file:////home/elison/Pictures/morning.jpg
00 12 * * * DISPLAY=:0 GSETTINGS_BACKEND=dconf /usr/bin/gsettings set org.gnome.desktop.background picture-uri file:////home/elison/Pictures/noon.jpg
回答5:
add export DISPLAY=:0 && export XAUTHORITY=/home/username/.Xauthority , where username is your ubuntu username. It should fix the X11 authorisation error.