Linux 带有 cron 的 Gsettings
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10374520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gsettings with cron
提问by Mateusz Jagie??o
I wrote a bash script that changes the wallpaper (for GNOME3).
我写了一个 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}"
Script executed in a terminal emulator (eg gnome-terminal) works great. During the execution by cron, or ttyX terminal getting the error:
在终端模拟器(例如 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
回答by Mateusz Jagie??o
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.
我找到了一些解决方案。当您导出文件 ~/.dbus/session-bus/* 中包含的变量 DBUS_SESSION_BUS_ADDRESS 时,dbus-launch 不会告诉更多有关错误的信息。然而,不是墙纸,而是人工制品。
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}"
回答by Elison Niven
To change your wallpaper through cron, just do this directly in your crontab : Execute crontab -e
要通过 cron 更改壁纸,只需直接在您的 crontab 中执行此操作:执行 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
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
00 12 * * * DISPLAY=:0 GSETTINGS_BACKEND=dconf /usr/bin/gsettings set org.gnome.desktop.background picture-uri file:////home/elison/Pictures/noon.jpg
回答by Arnon Weinberg
Tried this and it worked great for me:
试过这个,它对我很有用:
dbus-launch --exit-with-session gsettings set schema key value
Or from root cron:
或者从根 cron:
sudo -u user dbus-launch --exit-with-session gsettings set schema key value
回答by Vivek Pradhan
add export DISPLAY=:0 && export XAUTHORITY=/home/username/.Xauthority , where username is your ubuntu username. It should fix the X11 authorisation error.
添加 export DISPLAY=:0 && export XAUTHORITY=/home/username/.Xauthority ,其中 username 是您的 ubuntu 用户名。它应该修复 X11 授权错误。
回答by Radu R?deanu
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.
事实上,这个问题的发生是因为 cron 只使用了一组非常有限的环境变量。唯一一个负责以正确方式运行问题中的脚本的环境变量是DBUS_SESSION_BUS_ADDRESS
, not DISPLAY
or XAUTHORITY
orGSETTINGS_BACKEND
或其他东西。在这个答案中也很好地指出了这一事实。
But the problem in this answeris that there's no guaranteethat 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:
但是这个答案中的问题是不能保证目录中DBUS_SESSION_BUS_ADDRESS
该文件中的变量~/.dbus/session-bus/
会更新为当前 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-)
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}"
回答by thierrybo
Also see this solution that work for me: https://unix.stackexchange.com/questions/111188/using-notify-send-with-cron#answer-111190:
另请参阅此对我有用的解决方案: https: //unix.stackexchange.com/questions/111188/using-notify-send-with-cron#answer-111190:
You need to set the DBUS_SESSION_BUS_ADDRESS variable. By default cron does not have access to the variable. To remedy this put the following script somewhere and call it when the user logs in, for example using awesome and the run_once function mentioned on the wiki. Any method will do, since it does not harm if the function is called more often than required.
您需要设置 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
This creates a file containing the required Dbus evironment variable. Then in the script called by cron you import the variable by sourcing the script:
这将创建一个包含所需 Dbus 环境变量的文件。然后在 cron 调用的脚本中,通过获取脚本来导入变量:
if [ -r "$HOME/.dbus/Xdbus" ]; then
. "$HOME/.dbus/Xdbus"
fi
回答by Mach Seven
And finally the ugly, no cron at all (darn it)! Using other methods the changes get set in gconf, but the image does not change. Maybe it is because I am running Deepin's DDE (dde makes use of the same path, different key). Ugly for the rescue: A final attempt to make this saga work.
最后是丑陋的,根本没有 cron(该死的)!使用其他方法在 gconf 中设置更改,但图像不会更改。可能是因为我运行的是Deepin的DDE(dde使用的是同一个路径,不同的key)。丑陋的救援:使这个传奇成功的最后一次尝试。
With this script the wallpaper is changed every 420 seconds (7 minutes) in an endless loop picking a random wallpaper from one of 4 sets (or directories), acordingly to the time of the day or night.
使用此脚本,墙纸每 420 秒(7 分钟)更改一次,无限循环地根据白天或晚上的时间从 4 套(或目录)之一中随机选择墙纸。
I've created a .desktop file and added this .desktop file to the "~/.config/autostart". I've also created another pair script/desktop without the loop to be on my dock, so I can click on it and change it on fly too.
我创建了一个 .desktop 文件并将这个 .desktop 文件添加到"~/.config/autostart"。我还创建了另一对脚本/桌面,没有循环在我的 Dock 上,所以我可以点击它并即时更改它。
Set the ugly up: save the script as wallpaperdsomewhere and make it executable:
设置丑陋:将脚本保存为墙纸并使其可执行:
chmod +x wallpaperd
chmod +x 壁纸
Now create a folder called Wallpaperinside the Picturesdirectory. Inside this Wallpaper folder create 4 more folders with the names afternoon, duskdawn, morningand night. Put the image files you wish in those 4 directories.
现在在图片目录中创建一个名为墙纸的文件夹。在此墙纸文件夹中,再创建 4 个文件夹,名称分别为下午、黄昏、早晨和晚上。将您想要的图像文件放在这 4 个目录中。
mkdir -p ~/Pictures/Wallpaper/morning
mkdir ~/Pictures/Wallpaper/afternoon
mkdir ~/Pictures/Wallpaper/night
mkdir ~/Pictures/Wallpaper/duskdawn
mkdir -p ~/Pictures/Wallpaper/morning
mkdir ~/Pictures/Wallpaper/afternoon
mkdir ~/Pictures/Wallpaper/night
mkdir ~/Pictures/Wallpaper/duskdawn
wallpaperd
壁纸
#!/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
- Set the proper gsettings command for your desktop in script!
- 在脚本中为您的桌面设置正确的 gsettings 命令!
wallyd.desktop
wallyd.desktop
** Autostart Path: /home/YOUR_USER/.config/autostart/wallyd.desktop**
** 自动启动路径:/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
- Edit the script's path to match the path where you saved the script.
- 编辑脚本的路径以匹配您保存脚本的路径。
No loop
无循环
To create a desktop icon without the loop, only to change the wally and quit do this:
要创建没有循环的桌面图标,只需更改 wally 并退出,请执行以下操作:
Save the script as wallpaper(without the d at the end) and remove these lines:
将脚本另存为墙纸(末尾没有 d)并删除这些行:
for (( ; ; ))
do
done
Use the template above to create another .desktop file for this non looped wallpaperscript. Change the Nameand the Execpath for the non looped script.
使用上面的模板为这个非循环墙纸脚本创建另一个 .desktop 文件。更改非循环脚本的名称和执行路径。
Save this .desktop here:
在此处保存此 .desktop:
/usr/share/applications/wally.desktop
/usr/share/applications/wally.desktop
Drag it to your taskbar or dock. Click it and it will change the wallpaper on the fly.
将其拖到任务栏或停靠栏。单击它,它将动态更改墙纸。