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
Related videos on Youtube
Shaun
Updated on September 18, 2022Comments
-
Shaun over 1 year
I'm trying to change my wallpaper to a random image using Indrajith Indraprastham's suggested script here: How to change desktop background from command line in Unity?
When I run the script from a terminal window, the bg changes just fine, but when it's run from cron, I'm mailed this error:
(process:21901): dconf-WARNING **: failed to commit changes to dconf: Error spawning command line 'dbus-launch --autolaunch=00216c114dcf433c9bb9009985d607d6 --binary-syntax --close-stderr': Child process exited with code 1
I would appreciate any suggestions.
-
Jacob Vlijm about 8 years@Shaun You' re welcome! Glad it works :)
-
donquixote almost 8 yearsI am trying this with Cinnamon on Mint. When I run this from the cli directly, I get "dconf-WARNING *: failed to commit changes to dconf: The given address is empty". It used to work before (from cli), without the PID and DBUS_SESSION_. (But not from cron)
-
Jānis Elmeris over 7 years@donquixote Try
PID=$(pgrep -f 'gnome-session' | head -n1)
! -
wjandrea over 6 yearsYou can replace
DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)
with$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ)
Since grep gets the variable name too. source -
joseph sullivan over 5 yearsNot working on ubuntu 18.04
-
Warren Sergent over 5 years
pgrep gnome-session
may return more than one pid if multiple users are logged into the system (each running gnome-session). PerhapsEUID=$(id --real --user)
andPID=$(pgrep --euid $EUID gnome-session)
would be a way to get only the PID associated with the current user's gnome session. -
Steeve McCauley about 5 yearsOr perhaps $(pgrep -u $LOGNAME gnome-session)