Run Python (selenium) script in crontab

I read most of python / cron here on stackoverflow and still couldn't run my script. I realized that I need to run my script through the shell (using zsh and ipython by the way), but in fact I have no idea what to do: /

My simple code is:

In crontab -

*/1 * * * * ipython /home/usr/Data/progs/cron_test.py

My python script is

import pickle
    from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.google.com')

t=driver.current_url
pickle.dump(t,open('noreal','wb'))

I already tried some things, but to no avail:

#!python ../python etc
SHELL = /usr/bin/zsh
PATH =/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

Just because I donโ€™t know what the problem is, I am assuming something with running a python script through an interpreter, but I donโ€™t know what I am actually doing :)

A working solution would be great and I would really appreciate an explanation to accompany any solution so that I can understand why and how, and not just how it works! Thank you Bye!

Thanks for any help!

, python :

*/3 * * * * /usr/local/bin/ipython /home/user/Data/progs/RF/cron_test.py

:

[1;31m---------------------------------------------------------------------------[0m
[1;31mWebDriverException[0m                        Traceback (most recent call last)
[1;32m/usr/local/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc[0m in [0;36mexecfile[1;34m(fname, *where)[0m
[0;32m    176[0m             [1;32melse[0m[1;33m:[0m[1;33m[0m[0m
[0;32m    177[0m                 [0mfilename[0m [1;33m=[0m [0mfname[0m[1;33m[0m[0m
[1;32m--> 178[1;33m             [0m__builtin__[0m[1;33m.[0m[0mexecfile[0m[1;33m([0m[0mfilename[0m[1;33m,[0m [1;33m*[0m[0mwhere[0m[1;33m)[0m[1;33m[0m[0m
[0m
[1;32m/home/user/Data/progs/FB/cron_test.py[0m in [0;36m<module>[1;34m()[0m
[0;32m      9[0m [1;33m[0m[0m
[0;32m     10[0m [1;33m[0m[0m
[1;32m---> 11[1;33m [0mdriver[0m [1;33m=[0m [0mwebdriver[0m[1;33m.[0m[0mFirefox[0m[1;33m([0m[1;33m)[0m[1;33m[0m[0m
[0m[0;32m     12[0m [0mdriver[0m[1;33m.[0m[0mget[0m[1;33m([0m[1;34m'http://www.google.com'[0m[1;33m)[0m[1;33m[0m[0m
[0;32m     13[0m [1;33m[0m[0m

[1;32m/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.pyc[0m in [0;36m__init__[1;34m(self, firefox_profile, firefox_binary, timeout, capabilities, proxy)[0m
[0;32m     58[0m         RemoteWebDriver.__init__(self,
[0;32m     59[0m             command_executor=ExtensionConnection("127.0.0.1", self.profile,
[1;32m---> 60[1;33m             self.binary, timeout),
[0m[0;32m     61[0m             desired_capabilities=capabilities)
[0;32m     62[0m         [0mself[0m[1;33m.[0m[0m_is_remote[0m [1;33m=[0m [0mFalse[0m[1;33m[0m[0m

[1;32m/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.pyc[0m in [0;36m__init__[1;34m(self, host, firefox_profile, firefox_binary, timeout)[0m
[0;32m     45[0m         [0mself[0m[1;33m.[0m[0mprofile[0m[1;33m.[0m[0madd_extension[0m[1;33m([0m[1;33m)[0m[1;33m[0m[0m
[0;32m     46[0m [1;33m[0m[0m
[1;32m---> 47[1;33m         [0mself[0m[1;33m.[0m[0mbinary[0m[1;33m.[0m[0mlaunch_browser[0m[1;33m([0m[0mself[0m[1;33m.[0m[0mprofile[0m[1;33m)[0m[1;33m[0m[0m
[0m[0;32m     48[0m         [0m_URL[0m [1;33m=[0m [1;34m"http://%s:%d/hub"[0m [1;33m%[0m [1;33m([0m[0mHOST[0m[1;33m,[0m [0mPORT[0m[1;33m)[0m[1;33m[0m[0m
[0;32m     49[0m         RemoteConnection.__init__(

[1;32m/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.pyc[0m in [0;36mlaunch_browser[1;34m(self, profile)[0m
[0;32m     59[0m [1;33m[0m[0m
[0;32m     60[0m         [0mself[0m[1;33m.[0m[0m_start_from_profile_path[0m[1;33m([0m[0mself[0m[1;33m.[0m[0mprofile[0m[1;33m.[0m[0mpath[0m[1;33m)[0m[1;33m[0m[0m
[1;32m---> 61[1;33m         [0mself[0m[1;33m.[0m[0m_wait_until_connectable[0m[1;33m([0m[1;33m)[0m[1;33m[0m[0m
[0m[0;32m     62[0m [1;33m[0m[0m
[0;32m     63[0m     [1;32mdef[0m [0mkill[0m[1;33m([0m[0mself[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[0m

[1;32m/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.pyc[0m in [0;36m_wait_until_connectable[1;34m(self)[0m
[0;32m     98[0m                 raise WebDriverException("The browser appears to have exited "
[0;32m     99[0m                       [1;34m"before we could connect. The output was: %s"[0m [1;33m%[0m[1;33m[0m[0m
[1;32m--> 100[1;33m                       self._get_firefox_output())
[0m[0;32m    101[0m             [1;32mif[0m [0mcount[0m [1;33m==[0m [1;36m30[0m[1;33m:[0m[1;33m[0m[0m
[0;32m    102[0m                 [0mself[0m[1;33m.[0m[0mkill[0m[1;33m([0m[1;33m)[0m[1;33m[0m[0m

[1;31mWebDriverException[0m: Message: 'The browser appears to have exited before we could connect. The output was: Error: no display specified\n' 
+4
3

, , X-. script . .

:
, , firefox X, , .

 The output was: Error: no display specified\n' 

, , .

+3

, cron :

30 5 * * * export DISPLAY=:0; /home/me/good_morning.sh
+8

Install Xvfb or Xephyr or Xvnc. Also install the pyvirtualdisplay python module. This is for Ubuntu 14.04 and also tested on 16.04.

sudo apt-get install python-pip
sudo apt-get install xvfb xserver-xephyr vnc4server
sudo pip install pyvirtualdisplay
# optional
sudo apt-get install python-pil scrot
sudo pip install pyscreenshot

Now you can run Xvfb from your python script. It will start Xvfb when your script is executed. Add the following lines to your code.

from pyvirtualdisplay import Display
display = Display(visible=0, size=(1024, 768))
display.start()

Here is a sample code.

from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(1024, 768))
display.start()
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.title
browser.quit()

display.stop()

Now you can mention the program in your crontab. But add the path to ipython instead of ipython.

0 * * * * /usr/bin/python /home/usr/Data/progs/cron_test.py build
-1
source

All Articles