8

I'm running 12.04 with a custom XML background (modification on Day of Ubuntu) that changes based on time of day. I've noticed that there's a significant delay between when the changes are scheduled to take place in the XML file and when they actually show up on the background. I've also noticed that when I resume from suspend I don't get the correct background image either. I've found that cycling the wallpaper manually will fix this, and I've written a script to automate the process.

If I execute the script manually it works fine. However, when I schedule the script to run in cron, cron doesn't change the background. To make sure that the script was being run properly by cron, I had it create a directory in my home folder after running the background change, and the directory is created successfully, so I know cron is running and executing the script.

My script:

#!/bin/bash

sleep 5
gsettings set org.gnome.desktop.background picture-uri
file:///home/zak/Pictures/Wallpaper/DOU2.xml
sleep 1
gsettings set org.gnome.desktop.background picture-uri 
file:///home/zak/Pictures/Wallpaper/DOU.xml
sleep 1
mkdir /home/zak/iscronworking

exit

Is cron just not able to access gsettings? The job is on my user crontab so it shouldn't be running as root.

4 Answers 4

12

Apparently gsettings needs some variables to be set. Because CRON uses only a very restricted set of environment variables you must set them before your script. Use the following code in your CRON line.

30 */2 * * * DISPLAY=:0 GSETTINGS_BACKEND=dconf /your/path/your-script.sh

In the example the job is set to run every 2 hours on the 30th minute. I've tried to insert the variables into the script, for a cleaner line, with no result, if someone find a way to do that, let us know.

Stumbled with these settings in ArchLinux forums.

The above solution no longer works with Vivid.

The best way to get this to work is indeed to find DBUS_SESSION_BUS_ADDRESS variable, in the following script I'm using a for loop to do the job because using pidof of a specific application like gnome-session doesn't always work for me and the newers applications have a different DBUS ADDRESS probably because, in my particular case, I'm starting some daemons on boot with my user name. To effectively change the wallpaper I'm using dconf but you can also use gsettings. So tweak the script to your use case.

#!/bin/bash -e
user=$(whoami)

fl=$(find /proc -maxdepth 2 -user $user -name environ -print -quit)
while [ -z $(grep -z DBUS_SESSION_BUS_ADDRESS "$fl" | cut -d= -f2- | tr -d '\000' ) ]
do
  fl=$(find /proc -maxdepth 2 -user $user -name environ -newer "$fl" -print -quit)
done

export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS "$fl" | cut -d= -f2-)

if [ $# -gt 0 ]
then
  PICS_PATH=$1
else
  PICS_PATH="/home/public/Pictures/Wallpaper/"
fi

IMG=$(find -L $PICS_PATH -name "*.jpg" -o -name "*.png" | shuf -n1)
#gsettings set org.gnome.desktop.background picture-uri "file://${IMG}"
dconf write "/org/gnome/desktop/background/picture-uri" "'file://${IMG}'"

echo -e "$(date): ${IMG}" >> /tmp/wallch.log

in crontab add the following line to change wallpaper every even hour

0 */2 * * * /path/to-above-script.sh /path/to-wallpapers/
9

This is happen because cron uses only a very restricted set of environment variables. The only one environment variable that is responsible for running in the right way the script from the question when this is set as a cron job is DBUS_SESSION_BUS_ADDRESS, not DISPLAY or GSETTINGS_BACKEND or something else.

So, you must to export DBUS_SESSION_BUS_ADDRESS environment variable in your script. See more explanations in my answer here.

In the end, your script should look like:

#!/bin/bash

PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

sleep 5
gsettings set org.gnome.desktop.background picture-uri file:///home/zak/Pictures/Wallpaper/DOU2.xml
sleep 1
gsettings set org.gnome.desktop.background picture-uri file:///home/zak/Pictures/Wallpaper/DOU.xml
4
  • 3
    the only correct answer... Dec 31, 2013 at 12:20
  • 3
    Wish I had found this about 90 min ago
    – Mark
    Feb 25, 2014 at 16:46
  • 2
    If anyone has other users logging into their machine I recommend using PID=$(pgrep -u $USER gnome-session) to avoid changing other people's backgrounds.
    – Raab70
    May 28, 2015 at 19:37
  • @Raab70 I've tested this on Linux Mint and the variable $USER seems to be unavailable in a crontab job. (Also on mint the process to pgrep for is cinnamon-session, with the -f flag because of the length of the process name).
    – SimonT
    Feb 19, 2016 at 22:08
1

In Ubuntu 22.04 I had to do the following:

in crontab -e:

*/15 * * * * env DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/user/code/your_bash_script.sh

I didn't add the env before DISPLAY that's why it didn't work.

source: https://www.roboleary.net/2021/09/02/linux-change-wallpaper.html

1
  • Thanks for sharing. I was having a hard time getting the liveworldmap to update. Prepending the cronjob.sh command with env DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus sorted it for me too also on Ubuntu Jammy.
    – AnthonyK
    Mar 21, 2023 at 9:46
0

I started having the same problem about a month ago. I have also created a script which uses gsettings to change the desktop background in Gnome Shell/Unity.

Manually running this command immediately updates the desktop background. I also use cron to schedule execution of my script:

0,30 * * * * DISPLAY=:0.0 nextwall /home/serrano/Pictures/backgrounds/

As in your case, cron successfully runs the script and the new wallpaper URI is successfully set in gsettings. You can test this by running:

gsettings get org.gnome.desktop.background picture-uri

However, the desktop background is not updated when cron runs the script (this used to work until about a month ago). Interestingly, when I right click on my desktop and select "Change Desktop Background", the desktop background is suddenly updated to the correct image.

So for some reason changing the desktop background from cron no longer triggers Nautilus to update the desktop background. I suspect this is a bug, so I just filed a bug report for this issue: https://bugs.launchpad.net/ubuntu/+source/nautilus/+bug/1033034

Please click on the "Does this bug affect you?" link if you have the same problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .