这是我的脚本(randombg.py):
#!/usr/bin/env python
# -*- coding: utf8 -*-
import random
import subprocess
import os
BACKGROUND = '/home/david/wallpaper/dell2312'
IGNORE_FILES = ['/home/david/wallpaper/dell2312/.directory']
def enumerate():
global BACKGROUND
file_collections = []
for root, dirs, files in os.walk(BACKGROUND):
for file in files:
file_collections.append(os.path.join(root, file))
return file_collections
def randombg():
select_files = list(set(enumerate())-set(IGNORE_FILES))
subprocess.call(['feh', '--bg-scale', random.choice(select_files)])
def main():
while 1:
randombg()
if __name__ == '__main__':
main()
我已经运行chmod a+x randombg.py
并且它与python randombg.py
一起工作。请说它的路径为/path/to/randombg.py.
此外,运行/path/to/randombg.py
也有效。
但是,当我将它添加到crontab时,如下所示:
1 * * * * /path/to/randombg.py
或
01 * * * * python /path/to/randombg.py
或
01 * * * * /usr/bin/python /path/to/randombg.py
全部失败。
我无法弄清楚。谁能解释一下?
PS:ArchLinux
更多信息
当我运行ps aux|grep python
时,有时候我找不到randombg.py
。
来自crontab redirect stderr的附加日志:
import: unable to open X server `' @ error/import.c/ImportImageCommand/361.
import: unable to open X server `' @ error/import.c/ImportImageCommand/361.
import: unable to open X server `' @ error/import.c/ImportImageCommand/361.
/home/david/dotfiles/randombg.py: line 9: BACKGROUND: command not found
/home/david/dotfiles/randombg.py: line 10: IGNORE_FILES: command not found
/home/david/dotfiles/randombg.py: line 13: syntax error near unexpected token `('
/home/david/dotfiles/randombg.py: line 13: ` def enumerate():'
答案 0 :(得分:0)
尝试将subprocess.call
更改为
subprocess.call("export DISPLAY=:0; feh --bg-scale " + random.choice(select_files), shell=True)
这应该导出DISPLAY
变量,因为从crontab运行的脚本默认情况下无法访问环境变量。