Automatically change your wallpaper on Linux
Tags: linux, bash
Published: 2021-09-02 10:15:40


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](https://en.wikipedia.org/wiki/Cron) to run it regularly. First, I will look at doing this in [Gnome](https://en.wikipedia.org/wiki/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:

<img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdwrxdht72qn3p5knu9l.gif" alt="demo of changing wallpaper" width="600" height="322" loading="lazy"/>

## 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.

```bash
#!/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"
```

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](https://www.hivelocity.net/kb/what-is-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
```

If you are not familiar with cron, you can use [crontab.guru](https://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](https://github.com/l3ib/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:

```bash
nitrogen --random --set-scaled ~/pictures/wallpapers/
```

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:

```bash
0 * * * * nitrogen --random --set-scaled ~/pictures/wallpapers  
```

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](https://crontab.guru/) to help you make a different schedule.

## Final Word

Now, all you need to do is stock up on some wallpapers! [Wallhaven](https://wallhaven.cc/) 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](https://apod.nasa.gov/apod/), 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!