Pramod Jacob
Pramod Jacob

Reputation: 101

Running a bash script to change wallpapers via a cron job

I built a bash script that simply changes my wallpaper, using a directory within my Dropbox folder that contains all of the wallpaper images. It looks like this:

#!/bin/bash

# Select & display a random wallpaper from ~/Dropbox/wallpapers

echo "Changing wallpaper..."

# Get number of files (minus 1) in wallpapers
COUNT="$(ls -l ~/Dropbox/wallpapers | grep -v ^d | wc -l )"
ACTUAL_COUNT=$(($COUNT+1))
echo "Wallpaper count: ${ACTUAL_COUNT}"

# Select random number from 0 to COUNT
RAND=$(( ( RANDOM % $COUNT ) ))
RAND_PLUS_ONE=$(($RAND+1))
echo "Selecting wallpaper ${RAND_PLUS_ONE}"

# Get file path of random wallpaper
WALLPAPERS=(~/Dropbox/wallpapers/*)
WP_PATH=${WALLPAPERS[$RAND]}
echo "Wallpaper file path: ${WP_PATH}"

# Set wallpaper
echo "Rendering ${WP_PATH}"
eval "gsettings set org.gnome.desktop.background picture-uri 'file:///${WP_PATH}'"

Basically, I'm selecting a random image within a local directory ~/Dropbox/wallpapers and rendering it using gsettings.

I made this script executable and ran it from the console a few times, for good measure. It works. I named this script wallpaper_changer.sh and added it to ~/Dropbox/programming/scripts.

Now, I'm trying to add a cron job that executes the wallpaper changer script every few hours. To test it, I decided to run the script every minute for the time being. So I ran crontab -e and added the following:

MAILTO="[email protected]"
* * * * * /home/<user>/Dropbox/programming/scripts/wallpaper_changer.sh

Unfortunately, this doesn't seem to work. I tracked the logs sent to my email, and I get this as the output:

Changing wallpaper...
Wallpaper count: 92
Selecting wallpaper 86
Wallpaper file path: /home/<user>/Dropbox/wallpapers/witcher.png
Rendering /home/<user>/Dropbox/wallpapers/witcher.png

(process:29136): dconf-WARNING **: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY 

So my script is being executed, but the wallpaper isn't being changed. I tried to address the dconf-WARNING by following the instructions in this post, but to no avail.

Has anybody encountered this before? Is there something fundamental that I'm doing wrong here? Any help is greatly appreciated.

Solution

Turns out that the DBUS_SESSION_BUS_ADDRESS environment variable isn't set properly when running the script within the context of a cron job. I had to implement the solution from this Ask Ubuntu post to properly set the variable. Interestingly, it is set properly when I run the command directly from the terminal.

Upvotes: 3

Views: 1159

Answers (1)

xhienne
xhienne

Reputation: 6144

You need to set the DISPLAY environment variable to interact with an X environment.

Do either:

* * * * * DISPLAY=:0 /home/<user>/Dropbox/programming/scripts/wallpaper_changer.sh

Or:

DISPLAY=:0
* * * * * /home/<user>/Dropbox/programming/scripts/wallpaper_changer.sh

Adapt the value of DISPLAY if needed.

Upvotes: 2

Related Questions