Background not changing using gsettings from cron
Editing gsettings from cron; missing environment variable
If you run the script from your own environment (e.g. from a terminal window or from Startup Applications), a number of environment variables will be set. cron
however runs your script with a limited set of environment variables.
To edit gsettings
successfully from cron
, you need to set the DBUS_SESSION_BUS_ADDRESS
environment variable. You can do that by adding two lines to your script, as described here (and below).
Your script, including setting the needed variable
The script from here, edited to include the DBUS_SESSION_BUS_ADDRESS
environment variable, then becomes:
#!/bin/bash
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
DIR="/home/indra/Pictures/wallpapers"
PIC=$(ls $DIR/* | shuf -n1)
gsettings set org.gnome.desktop.background picture-uri "file://$PIC"
Related: Running .sh every 5 minutes