Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top