在Ubuntu 12.04中使用Python2.7 Crontab设置背景

Jam*_*mes 5 crontab python-2.7 ubuntu-12.04

我写了以下简单的python脚本,我打算在Ubuntu 12.04中设置为cron作业,每小时更换一次壁纸.当我从终端完全运行它时,脚本运行并更改壁纸.但是,当我设置cron作业时,我可以在syslog中看到cron作业已经运行,但壁纸没有改变?

#!/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))
Run Code Online (Sandbox Code Playgroud)

Jam*_*mes 2

在 cron 下运行 gsettings 似乎有问题。更改 os.system 命令以包含 DISPLAY=:0 GSETTINGS_BACKEND=dconf 即可解决问题。

os.system("DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '%s'" % (设置))