For Fun - Script to Rotate Wallpaper in Background

This is a fun little script that I wrote to rotate the wallpaper on my linux (ubuntu) machine. While not strictly on topic, you can use this as a template for your own scripts, such as those that monitor for file changes.

How do it…

We need two files, one to periodically rotate the wallpaper and the other to start the rotating script in the background and exit.

But first, setup a directory to put your wallpaper pictures in:

mkdir -p ~/Pictures/wallpaper

Here is the background script to change the wallpaper periodically (put in a file named rotateWallpaper):



while true
    image=`ls $WALLPAPER_DIR | shuf -n 1`
    echo "Changing to image $image"
    gsettings set org.gnome.desktop.background picture-uri "file://$WALLPAPER_DIR/$image"
    sleep 60

This is the wrapper script to run rotateWallpaper in the background (put in a file named launchWallpaper):


if [ ! -f /tmp/ ]; then
  echo $PROGRAM > /tmp/rotateWallpaper.error
  nohup $PROGRAM 2>/tmp/rotateWallpaper.error 1>/dev/null &
  echo $PID > /tmp/

exit 0

Make sure both files have execute permission for the current user:

chmod u+x ~/bin/rotateWallpaper
chmod u+x ~/bin/launchWallpaper

Then call launchWallpaper from someplace that loads when your machine starts up. I add the following to my bash .profile:


How it works…

There are a lot of ways to launch a script on startup (do a quick google search), but I choose to use my bash profile, because I always open a terminal on my machines. The launchWallpaper script will be called each time a new bash profile is started (such as opening a new terminal window), so the script needs to keep track if it is already started. For that reason the process ID of rotateWallpaper is stored as /tmp/ The first thing launchWallpaper does is check for the existence of the PID, only continuing if it doesn’t exist yet. This file will be automatically deleted when the machine is rebooted. Manually delete it if you terminate the PID and want to restart the script.

The launcher script then executes rotateWallpaper, sending errors to /tmp/rotateWallpaper.error (also cleared on reboot) and standard output to /dev/null. The rotateWallpaper is launched using nohup, so it keeps running if terminal is closed, and using (&) to put it in the background. The $! command is used to fetch the PID of the last run script and stored as /tmp/ Lastly, launchWallpaper exits without an error, so that whatever calls it is not blocked.

The rotateWallpaper script runs indefinitely with a sleep (specified in seconds) between each execution. To find an image the code lists every file in the wallpaper directory and randomly chooses one (don’t put anything else in that directory or errors will ensue). The gsettings is a gnome command that switches the wallpaper from the command line and is available on ubuntu.

Unfortunately, the gsettings and shuf commands don’t work on OSX, and none of this will work on windows. If you have similar scripts for other environments, please post in the comment section ^_^

There’s more…

Not interested in a script to rotate you’re wallpaper, but still want to learn something from this article. Well, there are a couple of good take aways. First, having a wrapper script to fetch and store the PID of a long-running script is very useful and something that unix processes have been doing forever. Second, by starting the rotateWallpaper script is the background and exiting without error from the wrapper script, we’ve successful put the long-running script in the background until reboot or it is manually killed. Together these two techniques can be used to start servers, run monitoring scripts, and many other useful tools.

Mint Launches

I have been working on Mint for the past year and am please to announce that we are out of BETA. We went live right after our presentation at the TechCrunch40 Conference this morning. If you are curious about your finances and want an effortless tool to help you manage, then Mint is for you:

Mint Homepage Screenshot