Automatically change Ubuntu wallpaper at a certain hour

  1. I'm not sure, but maybe you have to install dconf first

    sudo apt-get install dconf-cli
    
  2. Edit your crontab

    crontab -e
    
  3. Add an entry for each background image

    */5 4,5,6,7 * * *   /path/to/change_wallpaper '/path/of/your/wallpaper/for/4am'
    */5 8,8,9,10,11 * * *   /path/to/change_wallpaper '/path/of/your/wallpaper/for/8am'
    */5 12,13,14 * * *   /path/to/change_wallpaper '/path/of/your/wallpaper/for/12am'
    */5 15,16,17 * * *   /path/to/change_wallpaper '/path/of/your/wallpaper/for/3pm'
    */5 18,19 * * *   /path/to/change_wallpaper '/path/of/your/wallpaper/for/6pm'
    */5 20,21,22,23,0,1,2,3 * * *   /path/to/change_wallpaper '/path/of/your/wallpaper/for/8pm'
    
    • The interval is set to 5 minutes (*/5).
    • The lowest possible interval is 1 minute (*/1 or *)
  4. Save and close your crontab editor

  5. Create the script

    nano change_wallpaper
    
  6. Add the code below

    #!/bin/bash -e
    user=$(whoami)
    
    fl=$(find /proc -maxdepth 2 -user "$user" -name environ -print -quit)
    for i in {1..5}
    do
      fl=$(find /proc -maxdepth 2 -user "$user" -name environ -newer "$fl" -print -quit)
    done
    
    export DBUS_SESSION_BUS_ADDRESS
    DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS "$fl" | cut -d= -f2-)
    
    IMG="file://$1"
    if [ "$(gsettings get org.gnome.desktop.background picture-uri)" != "$FILE" ]; then
        dconf write "/org/gnome/desktop/background/picture-uri" "'file://${IMG}'"
       # gsettings set org.gnome.desktop.background picture-uri "'$IMG'"
    fi
    
    • The script works with dconf or gsettings. You can switch between both methods. Simply move the # in the front of the gsettings … line to the dconf … line
  7. Make it executable

    chmod +x change_wallpaper
    
  8. Test the script in your crontab

    • Edit your crontab again

      crontab -e
      
    • Add the (temporary) line below

      */1 * * * *   /path/to/change_wallpaper '/path/of/any/wallpaper'
      
    • Close the crontab editor

    • Wait a minute

  9. If the script works, remove the test entry

    • Edit your crontab again

      crontab -e
      
    • Remove the (temporary) line below

      */1 * * * *   /path/to/change_wallpaper '/path/of/any/wallpaper'
      
    • Close the crontab editor

Script partially taken from here