Here is my script(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.
Also, run /path/to/randombg.py
worked.
However, when I added it to crontab as below:
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
More infomation
When I run ps aux|grep python
, I can't find the randombg.py
while sometimes it appears.
Addtional logs from 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():'
Try to change your subprocess.call
to
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.