1

Linux crontab can not run with a graphical interface procedures? I completed a simple seleimun + chrome automatic login procedures in the test run time is OK to run, but in the crontab timing, I write , And wrote a simple control group, he did not run, what can I do?

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time,sys

driver = webdriver.Chrome()

driver.get("https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2fusers%2f7197440%2fa83533774%3ftab%3dtopactivity")

elem = driver.find_element_by_xpath('//*[@id="email"]')
elem.send_keys("******")

elem = driver.find_element_by_xpath('//*[@id="password"]')
elem.send_keys("***")

elem.send_keys(Keys.RETURN)
time.sleep(5)
name = time.ctime().replace(' ','-')+'.png'
driver.save_screenshot(name)
time.sleep(5)
print 'end',time.ctime()
driver.close()

crontab :

DISPLAY=:0 google-chrome

 */3 * * * * python ~/selenium_so.py >> log1.txt

 */1 * * * * date >> log.txt

I found this answer, but how do I practice him run selenium with crontab (python)

Thanks for the answer!

Community
  • 1
  • 1
luyishisi
  • 195
  • 11
  • There is no X session available in cron. Useva virtual framebuffer or PhantomJS instead of Chrome. – Klaus D. Nov 27 '16 at 03:40
  • Thank you for your answer, I will try to phantomjs to simulate the login, I would like to know if it is possible to close selemiun visual interface, – luyishisi Nov 27 '16 at 03:48

3 Answers3

2

If you want to launch something with a GUI you have to instruct cron where to send the output:

* * * * * export DISPLAY=:0 && (your instruction)

I'd need to build a test case to see if this will work with Selenium. I'm unclear if this export will apply to processes spawned by your python script instead of by cron itself. But in principle, this will allow you launch a process with a GUI.

There are some tricks to this, you need to be a user and not root generally, so make sure you put it in your user crontab instead of the root or sudo crontab.

Here are some example usages

Lost
  • 998
  • 10
  • 17
  • Thank you for your answer, I tried a success! Thank you very much! – luyishisi Nov 27 '16 at 04:27
  • */1 * * * * export DISPLAY=:0; python ~/selenium_so.py >> log1.txt – luyishisi Nov 27 '16 at 04:27
  • @luyishisi Glad it worked! That'll be useful for me in the future as well. If this turns out to be the best answer posted be sure to accept it as the answer. Good luck on your program – Lost Nov 27 '16 at 04:30
  • I have a doubt, since the show is set to 0 Why do I see the script when the operation of the visual interface? – luyishisi Nov 27 '16 at 04:34
  • I don't fully understand the question. Are you wanting to see the script outputs in a terminal? That won't happen because your cron instruction redirects the scripts outputs into a txt file. On that note, if you add "2>&1" to the end of your cron line it'll make sure you capture errors and standard outputs – Lost Nov 27 '16 at 04:38
  • I mean that since the show is set to 0 why the implementation of seleium script when there will be a visual interface pop – luyishisi Nov 27 '16 at 05:32
  • For Selenium to power most browsers it must have the graphical display. All Selenium is doing is automating clicks and typing for you. If you want something that runs in the background you need a headless browser like PhantomJS [see this question](http://stackoverflow.com/a/23898148/4777872) . If that is what you are looking for I can add it to the answer – Lost Nov 27 '16 at 05:44
0

I would recommend use Webmin It's a great Linux toolkit (Web interface)

Check this link: Scheduled crontab with Webmin

Tushar Niras
  • 3,654
  • 2
  • 22
  • 24
0

your app might use some environment variables you can store environment variables from /home/user1/.bashrc file

typeset -gx > /tmp/user1envvars

and /etc/crontab entry would look like

* * * * *   user1 /bin/bash -c 'source /tmp/user1envvars ; /path/to/your/app  args..'