LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (http://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Script for cycling desktop background in gnome not working when ran as root (http://www.linuxquestions.org/questions/linux-desktop-74/script-for-cycling-desktop-background-in-gnome-not-working-when-ran-as-root-784401/)

figure002 01-24-2010 09:25 AM

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?

explodingzebras 01-24-2010 10:42 AM

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/

tredegar 01-24-2010 11:33 AM

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.

figure002 01-24-2010 04:00 PM

Quote:

Originally Posted by explodingzebras (Post 3838775)
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! :D

Quote:

Originally Posted by tredegar (Post 3838812)
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 (Post 3838812)
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. :eek:

figure002 08-04-2010 06:59 AM

I finally found the fix for my problem.

Quote:

Originally Posted by tredegar (Post 3838812)
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/


All times are GMT -5. The time now is 01:29 AM.