[英]Gsettings with 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}"
在终端模拟器(例如 gnome-terminal)中执行的脚本效果很好。 在通过 cron 或 ttyX 终端执行期间得到错误:
** (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
经过多次尝试,我终于设法解决了这个问题。
事实上,这个问题的发生是因为 cron 只使用了一组非常有限的环境变量。 唯一一个负责以正确方式运行问题脚本的环境变量是DBUS_SESSION_BUS_ADDRESS
,而不是DISPLAY
或XAUTHORITY
或GSETTINGS_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
或者从根 cron:
sudo -u user dbus-launch --exit-with-session gsettings set schema key value
添加 export DISPLAY=:0 && export XAUTHORITY=/home/username/.Xauthority ,其中 username 是您的 ubuntu 用户名。 它应该修复 X11 授权错误。
要通过 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 设置 org.gnome.desktop.background 图片-uri file:////home/elison/Pictures/noon.jpg
另请参阅此对我有用的解决方案: https : //unix.stackexchange.com/questions/111188/using-notify-send-with-cron#answer-111190 :
您需要设置 DBUS_SESSION_BUS_ADDRESS 变量。 默认情况下,cron 无权访问该变量。 为了解决这个问题,将以下脚本放在某处并在用户登录时调用它,例如使用 awesome 和 wiki 上提到的 run_once 函数。 任何方法都可以,因为如果函数被调用的次数超过所需的次数,也不会造成损害。
#!/bin/sh
touch $HOME/.dbus/Xdbus
chmod 600 $HOME/.dbus/Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus
exit 0
这将创建一个包含所需 Dbus 环境变量的文件。 然后在 cron 调用的脚本中,通过获取脚本来导入变量:
if [ -r "$HOME/.dbus/Xdbus" ]; then
. "$HOME/.dbus/Xdbus"
fi
最后是丑陋的,根本没有 cron(该死的)! 使用其他方法在 gconf 中设置更改,但图像不会更改。 可能是因为我运行的是Deepin的DDE(dde使用的是同一个路径,不同的key)。 丑陋的救援:使这个传奇成功的最后一次尝试。
使用此脚本,墙纸每 420 秒(7 分钟)更改一次,无限循环地根据白天或晚上的时间从 4 组(或目录)之一中随机选择墙纸。
我创建了一个 .desktop 文件并将这个 .desktop 文件添加到"~/.config/autostart" 。 我还创建了另一对脚本/桌面,没有循环在我的 Dock 上,所以我可以点击它并即时更改它。
设置丑陋:将脚本保存为墙纸并使其可执行:
chmod +x 壁纸
现在在图片目录中创建一个名为墙纸的文件夹。 在此墙纸文件夹中,再创建 4 个文件夹,名称分别为下午、黄昏、早晨和晚上。 将您想要的图像文件放在这 4 个目录中。
mkdir -p ~/图片/壁纸/早上
mkdir ~/图片/壁纸/下午
mkdir ~/图片/壁纸/夜
mkdir ~/图片/壁纸/黄昏
#!/bin/bash
for (( ; ; ))
do
me="MyUser" # Change me!
morning="morning"
afternoon="afternoon"
dawn="duskdawn"
night="night"
dusk="duskdawn"
now="morning"
hour=$(date +%R | sed 's/\:.*//')
if [ "$hour" -ge 7 ] && [ "$hour" -lt 12 ]
then
now="morning"
elif [ "$hour" -ge 12 ] && [ "$hour" -lt 17 ]
then
now="afternoon"
elif [ "$hour" -ge 17 ] && [ "$hour" -lt 18 ]
then
now="duskdawn"
elif [ "$hour" -ge 18 ] && [ "$hour" -le 23 ]
then
now="night"
elif [ "$hour" -ge 0 ] && [ "$hour" -lt 6 ]
then
now="night"
elif [ "$hour" -ge 6 ] && [ "$hour" -lt 7 ]
then
now="duskdawn"
fi
imgPath="/home/$me/Pictures/Wallpaper/$now/"
imgFile=$(ls -1 $imgPath | shuf -n 1 | awk '{print $NF}')
export bgNow=""$imgPath$imgFile""
# Deepin desktop
/usr/bin/gsettings set com.deepin.wrap.gnome.desktop.background picture-uri "$bgNow"
# Gnome desktop
#/usr/bin/gsettings set org.gnome.desktop.background picture-uri "$bgNow"
sleep 420
done
** 自动启动路径:/home/YOUR_USER/.config/autostart/wallyd.desktop**
[Desktop Entry]
Categories=System;
Comment=Change Wallpapers Agent
Exec=/home/$USER/Bin/wallpaperd
Icon=computer
Name=Wally Daemon
NoDisplay=false
Terminal=false
Type=Application
要创建没有循环的桌面图标,只需更改 wally 并退出,请执行以下操作:
将脚本另存为墙纸(末尾没有 d)并删除这些行:
for (( ; ; ))
do
done
使用上面的模板为这个非循环墙纸脚本创建另一个 .desktop 文件。 更改非循环脚本的名称和执行路径。
在此处保存此 .desktop:
/usr/share/applications/wally.desktop
将其拖到任务栏或停靠栏。 单击它,它将动态更改墙纸。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.