Automatically change your wallpaper on Linux

Rob OLeary - Sep 2 '21 - - Dev Community

Having a beautiful wallpaper (desktop image) can boost your work environment aesthetic. I like the idea of swapping out the image regularly to keep things looking fresh.

I'm going to write a short script to set the desktop background to a random image, and use cron to run it regularly. First, I will look at doing this in Gnome, the default desktop environment for Ubuntu and some other distros. Then, I will look at an option that works in different desktop environments using a command-line tool instead (no scripting required).

This is what we want to happen on a schedule:

demo of changing wallpaper

Gnome

Gnome 3 has a gsettings command-line tool to view and change user settings. If you are using an older version of a distro, you may be using Gnome 2. If that is the case, you will need to use gconftool instead.

User data is stored as key-value pairs. We just need to find the right keys to set.

The keys we are interested in are:

  • picture-options: The rendering method. This decides what to do with the image if it is smaller or bigger than the screen resolution. I find that scaled works best most of the time: it centers the image and leaves it as its natural size if it is smaller, and it downscales the image if it is bigger.
  • picture-uri: The URI of the image file to display.
#!/bin/bash

folder="${HOME}/pictures/wallpapers"
pic=$(ls $folder/* | shuf -n1)

# values for picture-options: ‘none’, ‘wallpaper’, ‘centered’, ‘scaled’, ‘stretched’, ‘zoom’, ‘spanned’
gsettings set org.gnome.desktop.background picture-options scaled
gsettings set org.gnome.desktop.background picture-uri "file://$pic"
Enter fullscreen mode Exit fullscreen mode

Change the folder variable to point to a folder with images on your machine, and give the script a spin! Ensure the script has the 'execute' permission set by running sudo chmod +x /home/your-name/.local/bin/change-wallpaper.

Now, we just need to set up a cron job to run the script regularly. To make it work as a cron job, we need to set DBUS_SESSION_BUS_ADDRESS environment variable to ensure we can communicate with Gnome from another process. Open crontable with crontab -e and add the following lines to set-up a hourly job:

0 * * * * env DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/your-name/.local/bin/change-wallpaper
Enter fullscreen mode Exit fullscreen mode

If you are not familiar with cron, you can use crontab.guru to help you make a different schedule.

Other Desktop Environments

If you are using Gnome, KDE or XFCE, you have access to user settings to change the wallpaper, like I demonstrated above. However, if you are using a lightweight desktop manager such Openbox, you will find that there is no way that you can set the wallpaper. In this case, Nitrogen will come in handy. Nitrogen is a simple, lightweight application that allows you to change the desktop background with a nice set of options.

To get the same result as the previous section, we run the command:

nitrogen --random --set-scaled ~/pictures/wallpapers/
Enter fullscreen mode Exit fullscreen mode

Let's run it as a hourly cron job again. First, open crontable with crontab -e and add the following lines to set-up a hourly job:

0 * * * * nitrogen --random --set-scaled ~/pictures/wallpapers  
Enter fullscreen mode Exit fullscreen mode

Depending on your specific desktop environment, you also may need to set an environment variable to facilitate interprocess communication. I discussed this for Gnome in the last section.

If you are not familiar with cron, you can use crontab.guru to help you make a different schedule.

Final Word

Now, all you need to do is stock up on some wallpapers! Wallhaven has a good selection of free, high-quality wallpapers if you are searching for some.

I have seen some other people download an "image of the day" from some cool places, such as Nasa's Astronomy Picture of the Day, so they have a brand new wallpaper every day. You can extend the script yourself if you are interested in doing this.

That's it! Enjoy your virtual views!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player