Setting random backgrounds in LXDE.

29 June 2018

So, here's the situation:

On Windbringer, I habitually run LXDE as my desktop environment because it's lightweight and does what I need: It manages windows, gives me a menu, and stays out of my way so I can do interesting things.  For years I've been using a utility called GKrellm to implement not only system monitoring on my desktop (because I like to know what's going on), but to set and change my desktop background every 24 hours.  However, GKrellm has gotten somewhat long in the tooth and I've started using something different for realtime monitoring (but that's not the point of this post).  So, the question is, how do I set my background now?  Conky doesn't have that capability.

I tried a few of the old standbys like feh and nitrogen, but they didn't seem to work.  The reason for this appears to be that PCmanFM, which is both the file manager and the desktop... stuff... of LXDE.  By this, I refer to the desktop icons as well as the background image.  As it turns out, nothing I tried to change the background worked, and that is due to the fact that PCmanFM is a jealous desktop module and doesn't let other tools frob the settings it's in charge of.  After some tinkering, here's how I did it:

Short form: pcmanfm -w `ls -d -1 /home/drwho/backgrounds/* | shuf -n 1`

Long form (from inside to outside):

  • ls -d -1 /home/drwho/backgrounds/* - List all of the files in /home/drwho/backgrounds.  Show the full path to each file.  List everything in a single column.
  • | - Feed the output of the last command to the input of the next command.
  • shuf -n 1 - shuf is a little-known GNU Coreutils tool which randomly shuffles whatever things you give it.  It only returns one line of output, a randomly chosen image file.
  • The output of the previous two commands (captured between back-ticks) is passed to...
  • pcmanfm -w - Set the current desktop background to whatever filename is passed on the command line as a free action.

To set an initial background when I log in, I added the following command to my ~/.config/lxsession/LXDE/autostart file: @pcmanfm -w `ls -d -1 /home/drwho/backgrounds/* | shuf -n 1`

This means that the command will run every time my desktop starts up.  The @ symbol tells lxsession to re-run the command if it ever crashes.  However, how do I change my background periodically?

The easiest way to set that up was to set a cron job that runs every day.  Every user gets their own set of cron jobs (called a crontab) so you don't need any particular privileges to do this (unless your machine's really locked down).  If you've never set a cronjob before, the command I used was this: crontab -e

My cronjob looks like this: 00 10 * * * pcmanfm -w `ls -d -1 /home/drwho/backgrounds/* | shuf -n 1`

"At 10:00 hours every day, run the following command..."

And there you have it.  One randomly set desktop background in LXDE.

Incidentally, if you're curious about all the nifty things you can do with cron, I recommend playing around at crontab.guru, it's an online editor for crontab settings.  It's good for experimenting in such a way that you don't have to worry about messing up your system, and it's also handy for figuring out particularly arcane cronjobs.