2012-10-03 10 views
5

मैंने निम्नलिखित सरल पायथन लिपि लिखी है जिसे मैं एक घंटे में वॉलपेपर बदलने के लिए उबंटू 12.04 में क्रॉन नौकरी के रूप में सेट करना चाहता था। जब मैं इसे टर्मिनल से पूरी तरह से चलाता हूं तो स्क्रिप्ट वॉलपेपर चलाता है और बदलता है। हालांकि जब मैं क्रॉन जॉब सेट करता हूं तो मैं क्रॉन जॉब चलाने के syslog में देख सकता हूं लेकिन वॉलपेपर नहीं बदलता है?उबंटू में पाइथन 2.7 क्रोंटैब के साथ पृष्ठभूमि सेट करना 12.04

#!/usr/bin/python 

import os 
import random 

directory = os.getcwd() + '/' 
files = os.listdir('.') 
random.shuffle(files) 
files.remove('.project') 
files.remove('.pydevproject') 
files.remove('background.py') 
background = files[0] 
setup = 'file://' + directory + background 

print setup 

os.system("gsettings set org.gnome.desktop.background picture-uri '%s'" % (setup)) 

उत्तर

2

यह क्रॉन तहत gsettings चल के साथ अपने एक समस्या लगती है। DISPLAY =: 0 GSETTINGS_BACKEND = dconf चाल को शामिल करने के लिए os.system कमांड को बदलना।

os.system ("प्रदर्शन =: 0 GSETTINGS_BACKEND = dconf gsettings सेट org.gnome.desktop.background चित्र-uri '% s'"% (सेटअप))

1

आपको अपनी स्क्रिप्ट की कार्य निर्देशिका बदलनी है। आप इस तरह crontab से यह लागू द्वारा यह कर सकते हैं:

cd /path/of/your/script && python scriptname.py 

या आप कुछ इस तरह कर रही अपनी स्क्रिप्ट में कर सकते हैं:

import os 

my_path = os.path.abspath(__file__) 
dir_name = os.path.dirname(my_path) 
os.chdir(dir_name) 
+0

धन्यवाद मैं, लेकिन दोनों की कोशिश की न तो काम किया। पृष्ठभूमि बदल नहीं है। – James

0

इसके अलावा के लिए एक सही रास्ता प्रदान करने के लिए पृष्ठभूमि छवि फ़ाइल और स्थापित करने के लिए आवश्यक वातावरण चर आप os.system() कॉल बिना अजगर से पृष्ठभूमि को बदल सकता है:

import os 
import urllib 
from gi.repository.Gio import Settings # pylint: disable=F0401,E0611 

def set_background(image_path, check_exist=True): 
    """Change desktop background to image pointed by `image_path`. 

    """ 
    if check_exist: # make sure we can read it (at this time) 
     with open(image_path, 'rb') as f: 
      f.read(1) 

    # prepare uri 
    path = os.path.abspath(image_path) 
    if isinstance(path, unicode): # quote() doesn't like unicode 
     path = path.encode('utf-8') 
    uri = 'file://' + urllib.quote(path) 

    # change background 
    bg_setting = Settings.new('org.gnome.desktop.background') 
    bg_setting.set_string('picture-uri', uri) 
    bg_setting.apply() # might be unnecessary 

Automatic background changer using Python 2.7.3 not working, though it should से

संबंधित मुद्दे