5

I have a python script that correctly sets the desktop wallpaper via gconf to a random picture in a given folder.

I then have the following entry in my crontab

* * * * * python /home/bolster/bin/change-background.py

And syslog correctly reports execution

Apr 26 14:11:01 bolster-desktop CRON[9751]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:12:01 bolster-desktop CRON[9836]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:13:01 bolster-desktop CRON[9860]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:14:01 bolster-desktop CRON[9905]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:15:01 bolster-desktop CRON[9948]: (bolster) CMD (python /home/bolster/bin/change-background.py)
Apr 26 14:16:01 bolster-desktop CRON[9983]: (bolster) CMD (python /home/bolster/bin/change-background.py)

But no desktopy changey, Any ideas?

Bolster
  • 7,460
  • 13
  • 61
  • 96
  • Does you see the value change in gconf-editor? Does the wallpaper change if you set the value from gconf-editor? – Thomas Apr 26 '10 at 13:24
  • yes and yes, as i said, the script works outside of cron calling as just ~/bin/change-background.py and python ~/bin/change-background.py – Bolster Apr 26 '10 at 13:28

3 Answers3

6

Your script depends on the DISPLAY environment variable, which is set when you execute the script from the shell in an X session, but unset when the script is run from cron.

Bolo
  • 11,542
  • 7
  • 41
  • 60
  • 2
    Please state your answer in the form of an answer :) – Bolster Apr 26 '10 at 13:31
  • 4
    @Andrew I though I did. Your question was "Any ideas?", I gave you the most probable reason given the limited information that you've presented. I've correctly guessed that the problem is not with cron or gconf, it is with a particular environment variable. Seeing that you're apparently fluent with *NIX and Python, I didn't feel the need to teach you how to set environment variables (for the record, it's `os.environ["DISPLAY"] = ...` in Python). – Bolo Apr 26 '10 at 21:15
2

As per Bolo's observation, I forgot about building in the DISPLAY into either the script or the crontab.

Easiest solution is to prepend the crontab with env DISPLAY=:0.0

so:

* * * * * env DISPLAY=:0.0 python /home/bolster/bin/change-background.py
Bolster
  • 7,460
  • 13
  • 61
  • 96
  • Errr, unless some comment was deleted: *his*, not *this*, so no need to wait 44 hours? (But well, that's [a discussion on Meta](http://meta.stackexchange.com/questions/47906/etiquette-for-dealing-with-hint-answers).) – Arjan Apr 26 '10 at 19:19
  • 1
    No, sorry, I think you miss understand, or i was not clear enough, I'm waiting for the community to decide; whichever has more upvotes by the time i *could* accept my own answer, will be accepted. Its only fair. – Bolster Apr 26 '10 at 19:42
2

To set the DISPLAY environment variable, I would put it directly in the crontab. Also, I would make the script executable and give it a proper header (#!/usr/bin/env python) so that it can be executed directly. Additionally, you can rely on the PWD being set to HOME when the crontab runs.

My crontab would look like this:

DISPLAY=:0.0
* * * * * bin/change-background.py

You can also set the PATH (in the same manner as DISPLAY) so that the bin/ is not even needed.


The main gotcha for setting environment in the crontab is that values are not variable-interpolated. For example, this not give the expected results:

PATH=$HOME/bin:$PATH
bukzor
  • 37,539
  • 11
  • 77
  • 111
  • Script is executable, has proper header, and Path is correctly set ( I have alot of things in ~/bin) but I just like explicit stating of whats going on, so having the 'export DISPLAY:0.0' inline in the crontab will remind me why its there in 6 months time :D Thank you for your interest. – Bolster Apr 26 '10 at 18:33