[英]Python script not work in crontab
Here is my script(randombg.py): 这是我的脚本(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()
I have run chmod a+x randombg.py
and it worked with python randombg.py
.Let's say its path is /path/to/randombg.py.
我已经运行了chmod a+x randombg.py
并且它与python randombg.py
一起工作。假设它的路径是/path/to/randombg.py.
Also, run /path/to/randombg.py
worked. 同样,运行/path/to/randombg.py
也可以。
However, when I added it to crontab as below: 但是,当我将其添加到crontab时,如下所示:
1 * * * * /path/to/randombg.py
or 要么
01 * * * * python /path/to/randombg.py
or 要么
01 * * * * /usr/bin/python /path/to/randombg.py
All failed. 全部失败。
I can't figure out. 我不知道。 Could anyone explain? 谁能解释?
PS: ArchLinux PS:ArchLinux
More infomation 更多信息
When I run ps aux|grep python
, I can't find the randombg.py
while sometimes it appears. 当我运行ps aux|grep python
,有时找不到randombg.py
。
Addtional logs from crontab redirect stderr: 来自crontab重定向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():'
Try to change your subprocess.call
to 尝试将您的subprocess.call
更改为
subprocess.call("export DISPLAY=:0; feh --bg-scale " + random.choice(select_files), shell=True)
This should export the DISPLAY
variable, as scripts run from crontab do not have access to environmental variables by default. 这应该导出DISPLAY
变量,因为默认情况下,从crontab运行的脚本无法访问环境变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.