15

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.

Shaun
  • 321
  • 1
    @Merri apart from the fact that the answers there do not solve the problem of OP (which wouldn't make it a dupe by the way), How would this be a dupe???? Same error does not mean it is the same question. – Jacob Vlijm Mar 10 '16 at 05:37

2 Answers2

20

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

Jacob Vlijm
  • 83,767
  • @Shaun You' re welcome! Glad it works :) – Jacob Vlijm Mar 07 '16 at 18:27
  • 2
    I 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) – donquixote Jun 23 '16 at 16:39
  • 4
    @donquixote Try PID=$(pgrep -f 'gnome-session' | head -n1)! – Jānis Elmeris Nov 05 '16 at 21:41
  • 1
    You 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 – wjandrea Nov 24 '17 at 02:17
  • Not working on ubuntu 18.04 – Bipin Chandra Tripathi Oct 05 '18 at 05:12
  • 2
    pgrep gnome-session may return more than one pid if multiple users are logged into the system (each running gnome-session). Perhaps EUID=$(id --real --user) and PID=$(pgrep --euid $EUID gnome-session) would be a way to get only the PID associated with the current user's gnome session. – unutbu Dec 15 '18 at 04:35
  • Or perhaps $(pgrep -u $LOGNAME gnome-session) – Steeve McCauley May 17 '19 at 10:31
1

The answer from @jacob-vlijm didn't work for me because of a few shell script issues (the $(id --real --user) command was returning more than one line, and the grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2- command had a null byte error).

Here's one that works without issues (at least for me):

#!/bin/bash

REAL_UID=$(id --real --user) PID=$(pgrep --euid $REAL_UID gnome-session | head -n 1) export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2- | sed -e "s/\x0//g")

DIR="/home/indra/Pictures/wallpapers" PIC=$(ls $DIR/* | shuf -n1) gsettings set org.gnome.desktop.background picture-uri "file://$PIC"

Cyril N.
  • 121
  • 4