简体   繁体   English

Python 脚本未从 crontab 执行

[英]Python script not executed from crontab

I am not able to execute the below attached python script via crontab.我无法通过 crontab 执行下面附加的 python 脚本。 When I run script directly from CLI it works just fine, I see just a quick action how browser is opened, everything is setup and then browser closes.当我直接从 CLI 运行脚本时,它工作得很好,我只看到一个快速操作如何打开浏览器,一切都设置好,然后浏览器关闭。 But when run in crontab, nothing happens.但是在 crontab 中运行时,什么也没有发生。 Crontab entry: crontab 条目:

* * * * * /full/path/to/script.py

I also tried:我也试过:

* * * * * /usr/bib/python /full/path/to/script.py

SCRIPT:脚本:

#!/usr/bin/python

from selenium import webdriver
from selenium.webdriver.support.ui import Select

browser = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver")
#open website
browser.get("http:website.php")
# find element in dropdown
Select(browser.find_element_by_name("ma")).select_by_value("817")
# confirm by clicking button
element = browser.find_element_by_xpath("/html/body/form/table/tbody/tr[2]/td[3]/input")
element.click()

# select dropdown 
Select(browser.find_element_by_name("status")).select_by_value("11")

#confirm dropdown
submit = browser.find_element_by_xpath("/html/body/form/table[3]/tbody/tr/td/input")
submit.click()

browser.close()

/var/log/syslog: /var/log/系统日志:

May  4 11:34:01 pavol-Vostro-15-3568 CRON[11685]: (root) CMD (/full/path/to/script.py)

I faced a problem similar to yours.我遇到了与您类似的问题。 Your job might be failing because it requires an X session since you're trying to open a web browser.您的工作可能会失败,因为它需要X session ,因为您正在尝试打开 web 浏览器。 You should put export DISPLAY=:0;你应该把export DISPLAY=:0; after the schedule in your cronjob, as in在您的 cronjob 中的计划之后,如

* * * * * export DISPLAY=:0; /usr/bib/python /full/path/to/script.py

If this doesn't work, you could try replacing :0 with the output of echo $DISPLAY in a graphical terminal.如果这不起作用,您可以尝试在图形终端中用echo $DISPLAY的 output 替换:0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM