使用 crontab (python) 运行 selenium [英] run selenium with crontab (python)

查看:24
本文介绍了使用 crontab (python) 运行 selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 python 脚本,它通过 selenium 调用 chrome 下一行.

I have a python script that calls chrome via selenium with the next line.

 ff = webdriver.Chrome('/home/user01/webScraping/CollectAndGo/chromedriver')

python 脚本是从 shell 脚本调用的.

The python script is called from a shell script.

python /home/user01/webScraping/CollectAndGo/cgcom.py > /home/user01/webScraping/CollectAndGo/cgcom.log 2>&1

当我从终端运行脚本或仅执行 .sh 文件时,它运行良好,但当我安排 crontab 作业时,它会因下一个错误而失败.

When I run the script from the terminal or just executing the .sh file it works perfectly but when I schedule a crontab job it fail with the next error.

   raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: u'unknown error: Chrome failed to start: exited abnormally
  (Driver info: chromedriver=2.9.248304,platform=Linux 3.5.0-36-generic x86_64)' 

该错误与此问题的第一行代码有关.有人知道为什么会发生这种情况吗?

The error is related to the first line of code of this question. Does anybody know why this could be happening?

推荐答案

尝试从 cron 启动浏览器最明显的问题是,即使您的机器上运行了 X,DISPLAY 环境变量未为从 crontab 运行的进程设置,因此从那里启动浏览器将失败.

The most evident problem with trying to launch a browser from cron is that even if you have X running on your machine, the DISPLAY environment variable is not set for processes running from your crontab so launching a browser from there will fail.

解决方案范围从琐碎到超级复杂.一个简单的解决方案是接受如果没有 X 运行您的脚本将不会运行并手动将 DISPLAY 设置为 :0,这是默认显示编号Ubuntu 启动的默认 X 服务器.

Solutions range from the trivial to the super sophisticated. A trivial solution would be to accept that your script won't run if there is no X running and manually set DISPLAY to :0, which is the default display number for the default X server that Ubuntu starts.

例如,如果我将此命令放在 crontab 行的 command 列中,Chrome 将毫无问题地启动:

For instance, if I put this command in the command column of a crontab line, Chrome starts without issue:

DISPLAY=:0 google-chrome

特定于用户的 crontab 文件中的完整行类似于:

The complete line in the a user-specific crontab file would be something like:

0 * * * *  DISPLAY=:0 google-chrome

如果您想运行通过 selenium 启动 chrome 的 python 脚本,该行将改为:

If you want to run a python script that starts chrome through selenium, the line would instead look like:

0 * * * *  DISPLAY=:0 python my_script.py

命令字符串只是按原样发送到外壳程序,因此在最后一个示例中,字符串 DISPLAY=:0 python my_script.py 将直接传递到外壳程序.将在命令开始时立即给出的变量赋值解释为设置环境变量是常见的 shell 语法.(dashbash 肯定是这种情况,其中之一可能是大多数安装中的默认 shell.)所以 shell 解释的命令设置环境变量 DISPLAY 到值 :0 然后运行 ​​python my_script.py.由于 python 从启动它的 shell 继承了它的环境,所以变量 DISPLAY 也是 :0 .

The command string is just sent as-is to the shell so in the last example the string DISPLAY=:0 python my_script.py would be just passed to the shell. It is common shell syntax to interpret a variable assignment given immediately at the start of the command as setting an environment variable. (It is certainly the case for dash and bash, one of which is likely to be the default shell in most installations.) So the command that the shell interprets sets the environment variable DISPLAY to the value :0 and then runs python my_script.py. Since python inherits its environment from the shell that started it, the variable DISPLAY is :0 for it too.

设置 DISPLAY=:0 就像我上面显示的那样为后面的命令设置变量 only.对于 crontab 执行的所有命令,也可以将 DISPLAY 设置为 :0.例如在以下特定于用户的 crontab 中:

Setting DISPLAY=:0 like I show above sets the variable only for the command that follows. It is also possible to set DISPLAY to :0 for all commands executed by the crontab. For instance in the following user-specific crontab:

DISPLAY=:0

30 * * * *  google-chrome
0  * * * *  python my_script.py

DISPLAY=:0google-chromepython my_script 的执行设置环境变量 DISPLAY.py

the line DISPLAY=:0 sets the environment variable DISPLAY both for the execution of google-chrome and python my_script.py

这篇关于使用 crontab (python) 运行 selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆