问题
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
回答1:
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
回答2:
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.
回答3:
that did the trick for me:
#!/bin/bash
export DISPLAY=:0
/usr/bin/phantomjs /home/pi/test.js
来源:https://stackoverflow.com/questions/24219473/starting-phantomjs-from-a-script-in-a-cronjob