Home > Uncategorized > calling gsettings from crontab

calling gsettings from crontab

Problem
I have a script that updates my desktop wallpaper every midnight with the Bing background image (see bing.py here).

This script worked fine, but a few months ago it stopped updating my desktop. Today I had some time and tracked down the problem to the gsettings command. The script sets the background with this external command:

gsettings set org.gnome.desktop.background picture-uri file:///absolute/path/to/image.jpg

When I launched the script manually, the background was refreshed. However, when it was called from crontab, the background was not refreshed. gsettings actually set the new image, but the background was not refreshed, so I still saw the old image. Wat?

Solution
I found the solution here. As Radu Rădeanu points out, the only environment variable that is required to run the script correctly as a cron job is the DBUS_SESSION_BUS_ADDRESS, not DISPLAY or XAUTHORITY or GSETTINGS_BACKEND or something else.

Here is how to set the value of DBUS_SESSION_BUS_ADDRESS:

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

Now my crontab file looks like this:

HOME=/home/jabba
DISPLAY=:0
PYTHONPATH=$PYTHONPATH:$HOME/python/lib/jabbapylib

0 0 * * * $HOME/bin.python/bing.sh

The source of bing.sh:

#!/usr/bin/env bash

HOME=/home/jabba
DISPLAY=:0
PYTHONPATH=$PYTHONPATH:$HOME/python/lib/jabbapylib
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

DIR=$HOME/Dropbox/python/lib/jabbapylib/jabbapylib/apps
$DIR/bing.py >>$HOME/bing.log.txt
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: