LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-24-2018, 07:28 AM   #1
iamtheliquor
LQ Newbie
 
Registered: Aug 2018
Posts: 2

Rep: Reputation: Disabled
Arrow Selenium with python, works via CLI, but not with cron


Hello,

I have the following simple python/selenium script, that when run from the CLI works absolutely fine. Opens firefox to the BBC homepage, waits 5 seconds, and then closes the window;

Code:
#!/usr/bin/env python

from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get("http://bbc.co.uk/")
time.sleep(5)
driver.quit()
It is called using the following bash script, which also works with no problem;

Code:
#!/usr/bin/env bash

export DISPLAY=:0
/usr/bin/python /home/wfs-test/Documents/browser-test-v2.py
However, when scheduling this with cron, it does not work. I can see the script is being run by cron okay, it just does not open the browser and perform the tasks as it should do. I know cron does not know about the display, which is why I have the following entry in the bash script;

Code:
export DISPLAY=:0
This should tell cron where the display is, but it still does not work.

I have also tried using xvfb with PyVirtualDisplay with the following code;

Code:
#!/usr/bin/env python

from selenium import webdriver
from pyvirtualdisplay import Display
import time

def main():
    with Display(visible=True, size=(1680, 1050)):

        driver = webdriver.Firefox()
        driver.get("http://bbc.co.uk/")
        time.sleep(5)
        driver.quit()

if __name__ == "__main__":
    main()
Again, this works with no issue when called from the CLI or via a bash script, but does not work when scheduled with cron. I have tried scheduling both as the local user and root, with no difference.

Any ideas?


Ubuntu 18.04.1
Python 2.7.15rc1
pip (9.0.1)
selenium (3.14.0)
PyVirtualDisplay (0.2.1)
xvfb 2:1.19.6-1ubuntu4
 
Old 08-24-2018, 08:55 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
you ought to implement some error handling and logging to find out which command failed and why.
 
Old 09-05-2018, 02:50 AM   #3
iamtheliquor
LQ Newbie
 
Registered: Aug 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
So I got a bit side tracked with some other work stuff but got back to this, and the solution turned out to be quite simple. Just in case anyone was interested, looks like cron was struggling to find things, so adding the following two lines to the top of the bash script has sorted it;

Code:
export DISPLAY=:0
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Should have known really, not the first time I've had this type of thing with cron. I think what threw me is I would normally just write a bash script and use the full path for commands to avoid this kind of thing, whereas this is calling a python script to do the work.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Python Selenium: Dealing with Active Directory Auth popup yakkmeister Linux - Security 3 11-29-2017 07:27 PM
Command works fine in CLI but not in cron Entropy1024 Linux - Newbie 5 10-31-2015 11:46 AM
[SOLVED] convert works from CLI, but doesn't work when cron'd in a .sh anon091 Linux - Newbie 11 10-21-2013 02:58 PM
PHP Pear Mail works from CLI but not from cron jevaba Programming 5 02-28-2012 09:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration