Gsettings with cron

前端 未结 7 1817
北恋
北恋 2020-12-05 13:48

I wrote a bash script that changes the wallpaper (for GNOME3).

#!/bin/bash

# Wallpaper\'s directory.
dir=\"${HOME}/images/wallpapers/\"

# Random wallpaper.         


        
相关标签:
7条回答
  • 2020-12-05 14:14

    Also see this solution that work for me: 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.

    #!/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:

    if [ -r "$HOME/.dbus/Xdbus" ]; then
      . "$HOME/.dbus/Xdbus"
    fi
    
    0 讨论(0)
  • 2020-12-05 14:21

    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

    0 讨论(0)
  • 2020-12-05 14:24

    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.

    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.

    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.

    Set the ugly up: save the script as wallpaperd somewhere and make it executable:

    chmod +x wallpaperd

    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. Put the image files you wish in those 4 directories.

    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!

    wallyd.desktop

    ** Autostart Path: /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:

    Save the script as wallpaper (without the d at the end) and remove these lines:

    for (( ; ; ))
    do
    
    done
    

    Use the template above to create another .desktop file for this non looped wallpaper script. Change the Name and the Exec path for the non looped script.

    Save this .desktop here:

    /usr/share/applications/wally.desktop

    Drag it to your taskbar or dock. Click it and it will change the wallpaper on the fly.

    0 讨论(0)
  • 2020-12-05 14:26

    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

    0 讨论(0)
  • 2020-12-05 14:29

    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}"
    
    0 讨论(0)
  • 2020-12-05 14:35

    add export DISPLAY=:0 && export XAUTHORITY=/home/username/.Xauthority , where username is your ubuntu username. It should fix the X11 authorisation error.

    0 讨论(0)
提交回复
热议问题