用cron进行设置

我写了一个bash脚本来改变壁纸(对于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}" 

在terminal模拟器(例如gnome-terminal)中执行的脚本效果很好。 在cron执行期间,或者ttyXterminal出现错误:

 ** (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 

Solutions Collecting From Web of "用cron进行设置"

最后,我经过多次尝试,设法解决了这个问题。

事实上,问题的发生是因为cron只使用了一组非常有限的环境变量。 只有一个环境变量负责以正确的方式将脚本从设置为cron作业的问题DBUS_SESSION_BUS_ADDRESS ,而不是DISPLAYXAUTHORITYGSETTINGS_BACKEND或其他。 这个事实在这个答案中也被指出了很好。

但是这个答案的问题在于, 不能保证 ~/.dbus/session-bus/目录下该文件的DBUS_SESSION_BUS_ADDRESS变量被更新为当前gnome会话的当前值。 为了解决这个问题,一个方法是在当前的gnome会话中找到一个进程的PID,并从它的环境中获取dbus地址。 我们可以这样做:

 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-) 

话虽如此,脚本应该是这样的:

 #!/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}" 

我找到了一些解决方案 当输出文件〜/ .dbus / session-bus / *中包含的变量DBUS_SESSION_BUS_ADDRESS时,dbus-launch不会告诉更多关于错误的信息。 然而,而不是壁纸有文物。

添加的代码:

 sessionfile=`find "${HOME}/.dbus/session-bus/" -type f` export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'` 

现在这个脚本看起来像这样:

 #!/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}" 

试过这个,它对我很好:

 dbus-launch --exit-with-session gsettings set schema key value 

或从root cron:

 sudo -u user dbus-launch --exit-with-session gsettings set schema key value 

信用: http : //php.mandelson.org/wp2/?p=565

要通过cron更改壁纸,只需在crontab中直接执行此操作:执行crontab -e

添加这样的行:

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

添加export DISPLAY =:0 && export XAUTHORITY = / home / username / .Xauthority,其中username是你的ubuntu用户名。 它应该修复X11授权错误。