figure002 |
01-23-2010 07:06 PM |
Python script cannot reach X server when executed by cron
Hello all. So I have yet another annoying problem.
This is the case. I run openSUSE 11.2 with GNOME. I want my desktop wallpaper to automatically cycle on a fixed interval. So I found this script (change-background.py), edited it to match my needs, and put it in my home bin directory. The script runs fine.
change-background.py
Code:
#!/usr/bin/env python
#
# change-background.py
#
#
# A script to change to a random background image
#
#(c) 2004, Davyd Madeley <davyd@madeley.id.au>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or(at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
backgrounds = "/home/serrano/wallpapers"
import sys
import gconf
import os
import random
import mimetypes
def get_files_recursively(rootdir):
"""Recursively get a list of files from a folder."""
fileList = []
for root, subFolders, files in os.walk(rootdir):
for file in files:
fileList.append(os.path.join(root,file))
return fileList
# Get the files from the backgrounds folder.
dir_items = get_files_recursively(backgrounds)
# Check if the background items are actually images. Approved files are
# put in 'items'.
items = []
for item in dir_items:
mimetype = mimetypes.guess_type(item)[0]
if mimetype and mimetype.split('/')[0] == "image":
items.append(item)
# Get a random background item from the file list.
item = random.randint(0, len(items) - 1)
# Create a gconf object.
client = gconf.client_get_default()
# Get the current background used by GNOME.
current_bg = client.get_string("/desktop/gnome/background/picture_filename")
# Make sure the random background item isn't the same as the background
# currently being used.
while(items[item] == current_bg):
item = random.randint(0, len(items) - 1)
# Finally, set the new background.
client.set_string("/desktop/gnome/background/picture_filename", items[item])
sys.exit()
So the next step, is to make it run, let's say every minute (to test it first). We can use 'cron' for this job. So this is what I did:
Code:
serrano@saibot:~> sudo crontab -u serrano -e
In vim, added the following two lines:
* * * * * touch /home/serrano/CRON_WORKS.txt
* * * * * /home/serrano/bin/change-background.py
In vim, save and exit.
crontab: installing new crontab
If everything works ok, the wallpaper should now change every minute. But everything is not ok. Cron works fine, because I can tell from the file 'CRON_WORKS.txt' being created in my home directory. But the wallpaper didn't change.
I think the problem lies in the fact that cron runs 'change-background.py' as root. And that's where the 'change-background.py' stops working:
Code:
serrano@saibot:~> su root
Password:
saibot:/home/serrano # change-background.py
Traceback (most recent call last):
File "/home/serrano/bin/change-background.py", line 61, in <module>
current_bg = client.get_string("/desktop/gnome/background/picture_filename")
glib.GError: Failed to contact configuration server; some possible causes are that you need to enable TCP/IP networking for ORBit, or you have stale NFS locks due to a system crash. See http://projects.gnome.org/gconf/ for information. (Details - 1: Failed to get connection to session: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.)
saibot:/home/serrano #
Does anyone know how I can make the script working, so it works even when ran as root? Or is there a way to make cron run the script as a different user?
|