Ubuntu – How to cycle through grub background images every boot

bashbootcrongrub2

Final answer is already posted

Before reading the entire question note the answer below.

Short version of question

In Bash how can I find the next item in this file?

  • …/640×480-a.jpg
  • …/640×480-b.jpg
  • …/640×480-c.jpg

When the current item is embedded in this string?

GRUB_BACKGROUND="/home/rick/Pictures/Wallpaper/640x480-a.jpg"

If the current item is the last file entry, then the first file entry needs to be picked. The new entry needs to be updated in grub for the next boot.

What has been done so far

I have multiple background images for Grub. Manually editing /etc/default/grub to change the background image and then running sudo update-grub is time consuming and I'm likely to forget to do it. I would like it automatically done every boot / reboot.

I've setup a cron job called /etc/cron.d/cycle-grub-background containing:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@reboot   root    /usr/local/bin/cron-reboot-cycle-grub-background

I trust this job will run every reboot and not daily because sometimes the laptop isn't rebooted for many days and I don't want to skip over the available grub background images.

Ignore the code below filled with assumptions and dangerous errors

The following code is only provided for historical reference. Please do not use any of it as it is badly broken and of poor design.

The file /usr/local/bin/cron-reboot-cycle-grub-background contains:

#!/bin/bash

# NAME: cron-reboot-cycle-grub-background
# DATE: February 18, 2017
# PATH: /usr/local/bin/

# DESC: Cycle through available 640x480 wallpaper for grub background

# Get list of all grub images when 1920x1080 monitor has been downgraded
# to more readable 640x480 pixel format
ls /home/rick/Pictures/Wallpaper/640x480* > /tmp/grub-backgrounds

# Find this boot grub background image name
cat /etc/default/grub | grep BACKGROUND > /tmp/grub-background

# case? to find current background in backgrounds?
# Can we run systemd inhibit lock to prevent shutdown or reboot now?
# sed? to change to next background in /etc/default/grub

# Copy /etc/default/grub to /boot/grub/grub.cfg is the same as
# running sudo update-grub with out the 12 second delay

cp /boot/grub/grub.cfg /boot/grub/grub.cfg~
cp /etc/default/grub   /boot/grub/grub.cfg
# Can we release systemd inhibitor now?

# Now next reboot will have new background image
exit 0

Let's say the file /tmp/grub-backgrounds contains:

640x480-a.jpg
640x480-b.jpg
640x480-c.jpg

Let's say the file /tmp/grub-background contains:

GRUB_BACKGROUND="/home/rick/Pictures/Wallpaper/640x480-a.jpg"

Then the next picture on the list would be ...640x480-b.jpg.

But if the the current picture is ...c.jpg then the next picture resets to beginning of list and should be ...a.jpg.

I need to square this circle as it were. I think some sort of file case followed by sed is in order (as you can see by the code comments) but I can't quite wrap my mind around it.

As always, I'm grateful for any and all ideas.

Best Answer

A simple script, run from any of your system's startup scripts, can allow stepping through the wallpaper images in a particular folder, one step per restart. It's much faster than using update-grub and much simpler logic, plus it never needs to run update-grub. The downsides are that if you want to add an image to the list, you have to edit the script or write a slightly more complex one to handle variable list length, and you can't tell from the file names what image is in each file.

mv wallpaper3.png wallpaperx.png
mv wallpaper2.png wallpaper3.png
mv wallpaper1.png wallpaper2.png
mv wallpaperx.png wallpaper1.png

Even with a list of dozens of files, a longer, real-world version of this script ought to execute in a fraction of a second on an SSD, or in a couple seconds on a platter drive. It's simple, the logic is obvious, and you won't wonder, months or years later, what this script called by your startup is supposed to do.

Related Question