Q:

Setting background with Python2.7 Crontab in Ubuntu 12.04

I've written the following simple python script which I intended to set as a cron job in Ubuntu 12.04 to change the wallpaper once an hour. The script runs and changes the wallpaper when I run it from a terminal perfectly. However when I set the cron job up I can see in syslog the cron job has run but the wallpaper doesnt change?
#!/usr/bin/python

import os
import random

directory = os.getcwd() + '/'
files = os.listdir('.')
random.shuffle(files)
files.remove('.project')
files.remove('.pydevproject')
files.remove('background.py')
background = files[0]
setup = 'file://' + directory + background

print setup

os.system("gsettings set org.gnome.desktop.background picture-uri '%s'" % (setup))

A:

It seems its a problem with running gsettings under cron. Changing the os.system command to include DISPLAY=:0 GSETTINGS_BACKEND=dconf does the trick.
os.system("DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '%s'" % (setup))

