简体   繁体   English

带有 cron 的 Gsettings

[英]Gsettings with cron

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.在终端模拟器(例如 gnome-terminal)中执行的脚本效果很好。 During the execution by cron, or ttyX terminal getting the error:在通过 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

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.事实上,这个问题的发生是因为 cron 只使用了一组非常有限的环境变量。 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.唯一一个负责以正确方式运行问题脚本的环境变量是DBUS_SESSION_BUS_ADDRESS ,而不是DISPLAYXAUTHORITYGSETTINGS_BACKEND或其他东西。 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.但是这个答案中的问题是无法保证~/.dbus/session-bus/目录中该文件中的DBUS_SESSION_BUS_ADDRESS变量更新为当前 gnome 会话的当前值。 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.要解决这个问题,一种方法是在当前 gnome 会话中找到进程的 PID,并从其环境中获取 dbus 地址。 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}"

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.当您导出文件 ~/.dbus/session-bus/* 中包含的变量 DBUS_SESSION_BUS_ADDRESS 时,dbus-launch 不会告诉更多有关错误的信息。 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}"

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

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

add export DISPLAY=:0 && export XAUTHORITY=/home/username/.Xauthority , where username is your ubuntu username.添加 export DISPLAY=:0 && export XAUTHORITY=/home/username/.Xauthority ,其中 username 是您的 ubuntu 用户名。 It should fix the X11 authorisation error.它应该修复 X11 授权错误。

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 设置 org.gnome.desktop.background 图片-uri file:////home/elison/Pictures/noon.jpg

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.您需要设置 DBUS_SESSION_BUS_ADDRESS 变量。 By default cron does not have access to the variable.默认情况下,cron 无权访问该变量。 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.为了解决这个问题,将以下脚本放在某处并在用户登录时调用它,例如使用 awesome 和 wiki 上提到的 run_once 函数。 Any method will do, since it does not harm if the function is called more often than required.任何方法都可以,因为如果函数被调用的次数超过所需的次数,也不会造成损害。

#!/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.这将创建一个包含所需 Dbus 环境变量的文件。 Then in the script called by cron you import the variable by sourcing the script:然后在 cron 调用的脚本中,通过获取脚本来导入变量:

if [ -r "$HOME/.dbus/Xdbus" ]; then
  . "$HOME/.dbus/Xdbus"
fi

And finally the ugly , no cron at all ( darn it )!最后是丑陋的,根本没有 cron(该死的)! Using other methods the changes get set in gconf, but the image does not change.使用其他方法在 gconf 中设置更改,但图像不会更改。 Maybe it is because I am running Deepin's DDE (dde makes use of the same path, different key).可能是因为我运行的是Deepin的DDE(dde使用的是同一个路径,不同的key)。 Ugly for the rescue: A final attempt to make this saga work.丑陋的救援:使这个传奇成功的最后一次尝试。

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" .我创建了一个 .desktop 文件并将这个 .desktop 文件添加到"~/.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.我还创建了另一对脚本/桌面,没有循环在我的 Dock 上,所以我可以点击它并即时更改它。

Set the ugly up: save the script as wallpaperd somewhere and make it executable:设置丑陋:将脚本保存为墙纸并使其可执行:

chmod +x wallpaperd chmod +x 壁纸

Now create a folder called Wallpaper inside the Pictures directory.现在在图片目录中创建一个名为墙纸的文件夹。 Inside this Wallpaper folder create 4 more folders with the names afternoon , duskdawn , morning and night .在此墙纸文件夹中,再创建 4 个文件夹,名称分别为下午黄昏早晨晚上 Put the image files you wish in those 4 directories.将您想要的图像文件放在这 4 个目录中。

mkdir -p ~/Pictures/Wallpaper/morning mkdir -p ~/图片/壁纸/早上
mkdir ~/Pictures/Wallpaper/afternoon mkdir ~/图片/壁纸/下午
mkdir ~/Pictures/Wallpaper/night mkdir ~/图片/壁纸/夜
mkdir ~/Pictures/Wallpaper/duskdawn mkdir ~/图片/壁纸/黄昏

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 wallpaper script.使用上面的模板为这个非循环墙纸脚本创建另一个 .desktop 文件。 Change the Name and the Exec path for the non looped script.更改非循环脚本的名称执行路径。

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.单击它,它将动态更改墙纸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM