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):

#!/bin/sh

WALLPAPER_DIR=~/Pictures/wallpaper

while true
do
    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
done

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

#!/bin/bash

if [ ! -f /tmp/rotateWallpaper.pid ]; then
  PROGRAM=/usr/local/google/home/msnider/bin/rotateWallpaper.sh
  echo $PROGRAM > /tmp/rotateWallpaper.error
  nohup $PROGRAM 2>/tmp/rotateWallpaper.error 1>/dev/null &
  PID=$!
  echo $PID > /tmp/rotateWallpaper.pid
fi

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:

#...
~/bin/launchWallpaper
#...

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/rotateWallpaper.pid. 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/rotateWallpaper.pid. 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.

Things Web Application Monitoring Can Pick Up From Casino Security

Security exists in order to protect the important things. While network security is constantly being upgraded in order to fend off attackers, it could actually learn a lesson or two from the surveillance of a casino floor. Web-based security, especially for pay-to-play sites, uses multiple layers of protection. Betfair, currently the biggest Internet betting exchange, is one of the providers that use a 2-step authentication, intrusion detection systems, and other methods ...

Simplifying Google Play Games API

Previously we covered Using Google Play Games on the Web and how Google Play Games services[3] can be used for web games[1]. There was a lot of interest on that article, mostly about providing UI components, which is a project that I have started, when I am not working on the refactor of Gaming Engine - Snake Demo v2. However, before building a UI, the API needed to be cleaned up, ...

Using EMCAScript 6 Today

I have been avoiding writing about ECMAScript 6 (E6)[6] for the past couple of years. This is mostly because the standard was not finalized, and consequently most browsers/engines were implementing different and often unstable versions of the various new features. With the E6 spec stabilizing almost a year ago now[1] and the final release date scheduled sometime later this year[1], I expected most browsers/engines would have implemented much of it, with bake ...

Video Recording with MediaProjectionManager

Recording video on an Android device, as a developer, should be as easy as calling a platform-level API, possibly showing an intent for permission approval, before starting to streaming video. Unfortunately, we don’t live in an ideal world, and video recording is far more difficult than it should be. Fortunately, starting in Android Lollipop, there is a new API (MediaProjectionManager[1]) to make recording video easier. However, there is a remarkable amount of incomplete or ...

Karma Test Runner with QUnit in 10 Minutes

This is a ten minute primer for using Karma and QUnit to unit test JavaScript.

Getting ready

Install Karma[1], plugins, and Qunit[2].
 # Install Karma npm install karma --save-dev # Install plugins npm install karma-qunit karma-phantomjs-launcher --save-dev # Add global CLI npm install -g karma-cli # Install QUnit npm install --save-dev qunitjs 

How do it…

In your project directory, run the Karma initialization script:
 karma init ...

Object Pool Pattern in JavaScript

Now that we understand the Recycler Object for Object Pool Pattern, we can build the logic for managing the object pool. An object pool is a simple API that manages recycling and fetching recyclable objects. There are two common models for object pools, one with a fixed number of objects that errors if too many objects are requested, and the other (more flexible approach) is to use the object pool for a fixed number ...

Recycler Object for Object Pool Pattern

Creating or destroying an object is never free and JavaScript is no exception. Generally, the cost of creating/destroying an object in JIT-optimized JavaScript runtimes doesn't affect performance, but other languages will have a performance hit as well. The real culprit is the increase in your application's memory footprint (watch the memory tab in a developer tool while running the tests below for an illustration). This is why, in most cases, reusing a single object is ...

Start Using SRCSET IMG Attribute For Serving Device Specific Images

The article, JavaScript Low Resolution Image Replacer, discussed a JavaScript solution for replacing low resolution images with higher resolution ones after the page finishes loading all the initial resources (thereby reducing the load time of your pages). But what about loading different image sizes based on the resolution of the user’s device. We could write a JavaScript solution for this (and some developers have), but HTML 5 already introduces the concept of <img srcset[

JavaScript Low Resolution Image Replacer

This is a handy, yet very simple, widget I was hacking on to replace loading or low resolution images with higher resolution ones once the document has finished loading.

How do it…

The widget is built using the jQuery plugin system and here is the complete code:
 (function($) { var isLoaded = false, REPLACEMENT_CLASS = "replacement-class", REPLACEMENT_RCLASS = "replacement-rclass", REPLACEMENT_URL = "replacement-img", TIMER = 500; $(window).load(function() { isLoaded = true; }); function ...