LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices

Reply
 
Search this Thread
Old 01-24-2010, 09:25 AM   #1
figure002
Member
 
Registered: Nov 2008
Distribution: Ubuntu Maverick
Posts: 31

Rep: Reputation: 16
Script for cycling desktop background in gnome not working when ran as root


Hello people. I originally posted this problem in the Programming subforum, but got no replies so far. So I decided to post it here as well.

My problem is as follows. I run openSUSE 11.2 with GNOME 2.28 and I want my desktop background to automatically cycle randomly on a fixed interval. So I found this script (change-background.py), edited it to fit my needs, and put it in my home bin directory. The script works the way it should.

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 randomly 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 (at least I think it is). 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 #
As you can see, the script returns an error when I run it as root, and this is probably the reason that it doesn't work with cron.

Does anyone know how I can fix the script so it works even when ran as root? Or is there a way to make cron run the script as a different user? Or is there a better/easier way to make the desktop background cycle in GNOME?
 
Old 01-24-2010, 10:42 AM   #2
explodingzebras
Member
 
Registered: Oct 2007
Location: UK
Distribution: Ubuntu
Posts: 63

Rep: Reputation: 18
or you could use wallpaper-tray

http://www.associatedcontent.com/art...our_linux.html

and many others:

http://www.makeuseof.com/tag/5-wallp...pps-for-linux/
 
Old 01-24-2010, 11:33 AM   #3
tredegar
Guru
 
Registered: May 2003
Location: London, UK
Distribution: Ubuntu 10.04, mostly
Posts: 6,007

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366
Who is the owner of CRON_WORKS.txt ?

But I think the problem is the cron job cannot access your $DISPLAY

cron is probably overkill for this really, what's wrong with something simpler:
Code:
while true; do /home/serrano/bin/change-background.py; sleep 60; done &
All that wallpaper-changing would drive me mad though.
 
Old 01-24-2010, 04:00 PM   #4
figure002
Member
 
Registered: Nov 2008
Distribution: Ubuntu Maverick
Posts: 31

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by explodingzebras View Post
or you could use wallpaper-tray
Sjeez! Why haven't I heard of this tool earlier? I've been looking for a tool like this for ages! Finally a decent wallpaper changer for GNOME, thanks for the tip!

Quote:
Originally Posted by tredegar View Post
Who is the owner of CRON_WORKS.txt ?

But I think the problem is the cron job cannot access your $DISPLAY

cron is probably overkill for this really,
The owner of 'CRON_WORKS.txt' is me (not root)! So I was wrong about cron running the jobs as root. Even though I got wallpaper-tray now, I'd still like to get this script working.

Could it be a security thing in cron, for it not letting jobs access $DISPLAY (whatever that is)? Is there a workaround for that?

Quote:
Originally Posted by tredegar View Post
what's wrong with something simpler:
Code:
while true; do /home/serrano/bin/change-background.py; sleep 60; done &
All that wallpaper-changing would drive me mad though.
I guess that would work as well, but it's not as nice as letting cron do the job for you. By the way, once I get the cron thing working, I would change it to once per hour. Every minute would drive me mad as well.
 
Old 08-04-2010, 06:59 AM   #5
figure002
Member
 
Registered: Nov 2008
Distribution: Ubuntu Maverick
Posts: 31

Original Poster
Rep: Reputation: 16
Lightbulb

I finally found the fix for my problem.

Quote:
Originally Posted by tredegar View Post
But I think the problem is the cron job cannot access your $DISPLAY
tredegar was right. The fix is to set your DISPLAY variable in crontab. So my crontab rule should be as follows:

Code:
* * * * * DISPLAY=:0.0 /home/serrano/bin/change-background.py
And now it works the way it should!

Thanks to mddirba for providing the fix on this thread: http://www.linuxquestions.org/questi...s-root-784285/
 
  


Reply

Tags
background, cron, desktop, gconf, gnome, python, script


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
[SOLVED] Python gconf not working when ran as root figure002 Programming 5 08-04-2010 06:41 AM
Running a command as another user in a bash script ran as root? camphor Programming 2 03-29-2009 03:11 PM
desktop icons and background dissapeared in Gnome suse 10.2 sickley Suse/Novell 3 10-13-2007 08:48 PM
Can't set desktop background image in Gnome TruckStuff Linux - General 1 07-18-2005 03:15 PM
Gnome 2 desktop background lag miknight Linux - Software 12 03-27-2003 02:46 AM


All times are GMT -5. The time now is 07:42 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