LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-23-2010, 07:06 PM   #1
figure002
Member
 
Registered: Nov 2008
Distribution: Ubuntu Maverick
Posts: 31

Rep: Reputation: 16
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?

Last edited by figure002; 08-04-2010 at 06:52 AM. Reason: Thread title was incorrect
 
Old 01-24-2010, 12:13 PM   #2
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
i don't know which cron you got, but it prorbably supports this;
Code:
* * * * * serrano touch /home/serrano/CRON_WORKS.txt
* * * * * serrano /home/serrano/bin/change-background.py
this will make them run as user serrano
 
Old 01-24-2010, 04:59 PM   #3
figure002
Member
 
Registered: Nov 2008
Distribution: Ubuntu Maverick
Posts: 31

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by ozanbaba View Post
i don't know which cron you got, but it prorbably supports this;
Code:
* * * * * serrano touch /home/serrano/CRON_WORKS.txt
* * * * * serrano /home/serrano/bin/change-background.py
this will make them run as user serrano
The cron jobs wouldn't work after the change. But it seems I was wrong about cron running the jobs as root. I checked the owner of 'CRON_WORKS.txt' and it was 'serrano', not root. So the problem can't be caused by the script being run as root.

I've posted the same problem in another thread, and someone mentioned that this might be caused by cron denying the job's access to $DISPLAY. I don't really know what that means though..
 
Old 01-25-2010, 02:40 AM   #4
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 671

Rep: Reputation: 94
Quote:
Originally Posted by figure002 View Post
and someone mentioned that this might be caused by cron denying the job's access to $DISPLAY. I don't really know what that means though..
$DISPLAY variable is defined by X to let programs know the display. if it's not there, your program can not reach X window
 
Old 08-03-2010, 11:40 AM   #5
mddirba
LQ Newbie
 
Registered: Aug 2010
Posts: 1

Rep: Reputation: 1
I ran into this problem as well. The fix is to set you DISPLAY variable in crontab. So your crontab should be as follows:

* * * * * serrano DISPLAY=:0.0 /home/serrano/bin/change-background.py
 
1 members found this post helpful.
Old 08-04-2010, 06:41 AM   #6
figure002
Member
 
Registered: Nov 2008
Distribution: Ubuntu Maverick
Posts: 31

Original Poster
Rep: Reputation: 16
Thumbs up

Quote:
Originally Posted by mddirba View Post
I ran into this problem as well. The fix is to set you DISPLAY variable in crontab. So your crontab should be as follows:

* * * * * serrano DISPLAY=:0.0 /home/serrano/bin/change-background.py
That did the trick! Thank you for providing the fix

I should note however that the username in the crontab rule is not necessary if you use the -u <username> switch to run crontab.
 
  


Reply

Tags
background, cron, gconf, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
GCONF Frustration! GazL Slackware 7 08-22-2008 06:57 PM
Can't install GCONF :/ AriciU Slackware 36 07-26-2007 12:51 PM
gconf error installing python program on mandriva crazypig Linux - Software 2 08-09-2005 09:30 AM
GConf Error syzuhdi Linux - General 0 05-22-2002 09:35 PM


All times are GMT -5. The time now is 07:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration