Starting phantomJS from a script in a cronjob

2019-01-25 13:17发布

I'm running a python script through a cronjob. I have a virtual environment and in the cronjob I'm running it through this virtual environment. When I run the script normally phantomJS starts as it should, but running it through a script in a cronjob I get this error. What is missing in the cronjob to be able to start phantomjs?

Message: 'Unable to start phantomjs with ghostdriver.' ; Screenshot: available via screen
Traceback (most recent call last):
  File "/home/scraper/superfish-extension/chrome_3day.py", line 96, in <module>
    main()
  File "/home/scraper/superfish-extension/chrome_3day.py", line 73, in main
    browser = use_phantomjs()
  File "/home/scraper/superfish-extension/chrome.py", line 81, in use_phantomjs
    browser = webdriver.PhantomJS()
  File "/home/scraper/.virtualenvs/superfish/lib/python2.6/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 50, in __init__
    self.service.start()
  File "/home/scraper/.virtualenvs/superfish/lib/python2.6/site-packages/selenium/webdriver/phantomjs/service.py", line 69, in start
    raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
WebDriverException: Message: 'Unable to start phantomjs with ghostdriver.' ; Screenshot: available via screen

3条回答
仙女界的扛把子
2楼-- · 2019-01-25 13:43

As phantom is probably installed in /usr/local/bin, you should add that dir to PATH in your crontab. The following should do the trick:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
查看更多
趁早两清
3楼-- · 2019-01-25 13:47

that did the trick for me:

#!/bin/bash
export DISPLAY=:0
/usr/bin/phantomjs /home/pi/test.js
查看更多
混吃等死
4楼-- · 2019-01-25 14:02

As an alternative to accepted answer, you can add the following line at the 1st line of crontab:

PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs

which specifies the path of PhantomJS before running cron tasks.

查看更多
登录 后发表回答