简体   繁体   中英

ruby cron selenium-webdriver error

I'm trying to run ruby code with cron job and I'm using whenever gem.

Here is my simple code in .rb file

require "watir"
require "selenium-webdriver"
browser = Watir::Browser.start('https://www.google.com',:firefox)
sleep 5 
browser.close

my crontab -l output is:

  PATH=/home/ba/bin:/home/ba/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

49 16 * * * /bin/bash -l -c 'which ruby >> /home/ba/Desktop/RoR/cron1.log 2>&1'

49 16 * * * /bin/bash -l -c 'which gem >> /home/ba/Desktop/RoR/cron1.log 2>&1'

49 16 * * * /bin/bash -l -c 'which rails >> /home/ba/Desktop/RoR/cron1.log 2>&1'

49 16 * * * /bin/bash -l -c 'which bundle >> /home/ba/Desktop/RoR/cron1.log 2>&1'

49 16 * * * /bin/bash -l -c 'cd /home/ba/Desktop/job_1 && ruby job_2.rb >> /home/ba/Desktop/RoR/cron1.log 2>&1'

# End Whenever generated tasks for: /home/ba/Desktop/RoR/site_p/config/schedule.rb at: 2018-04-12 16:47:41 +0400

cron1.log shows this:

/usr/local/bin/rails
/usr/local/bin/ruby
/usr/local/bin/gem
/usr/local/bin/bundle
/usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok': Process unexpectedly closed with status 1 (Selenium::WebDriver::Error::UnknownError)
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/http/common.rb:81:in `new'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/http/common.rb:81:in `create_response'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/http/default.rb:104:in `request'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/bridge.rb:164:in `execute'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/bridge.rb:97:in `create_session'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/firefox/marionette/driver.rb:50:in `initialize'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/firefox/driver.rb:31:in `new'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/firefox/driver.rb:31:in `new'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/common/driver.rb:52:in `for'
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver.rb:85:in `for'
    from /usr/local/lib/ruby/gems/2.5.0/gems/watir-6.10.3/lib/watir/browser.rb:48:in `initialize'
    from /usr/local/lib/ruby/gems/2.5.0/gems/watir-6.10.3/lib/watir/browser.rb:30:in `new'
    from /usr/local/lib/ruby/gems/2.5.0/gems/watir-6.10.3/lib/watir/browser.rb:30:in `start'
    from job_2.rb:3:in `<main>'
  • ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux] without rvm
  • Rails 5.1.6
  • Firefox 59.0.2 (64-bit)

    gem 'watir' gem 'selenium-webdriver'

same error comes when I try to run cron job whith rails runner

code runs fine from terminal. *problem is only when i try to run it with cron job ! *

Also I try without whenever gem. Even with fresh ubuntu 16, ruby and rails.

cron was losing DISPLAY environment variable while executing.

adding ENV['DISPLAY'] = ":0" this to my script fixed my case

details:

  1. I add this Selenium::WebDriver.logger.level = :debug to my script (thanks a lot @titusfortner )

new log:

2018-04-13 09:30:01 DEBUG Selenium Executing Process ["/home/ba/.local/bin/geckodriver", "--binary=/usr/bin/firefox", "--port=4444"]
1523597401454   geckodriver INFO    geckodriver 0.20.1
1523597401458   geckodriver INFO    Listening on 127.0.0.1:4444
1523597401713   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-profile" "/tmp/rust_mozprofile.cG0M1jGsI4Zn"
Error: no DISPLAY environment variable specified
/usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok': Process unexpectedly closed with status 1 (Selenium::WebDriver::Error::UnknownError)
    from /usr/local/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.11.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'

I saw this: Error: no DISPLAY environment variable specified

  1. because driver worked fine from shell and rails ci run this to find correct value for DISPLAY echo $DISPLAY or from rails c puts ENV['DISPLAY'] result was ":0"

  2. so I add this to my script and problem was solved

    ENV['DISPLAY'] = ":0"

thanks a lot for good advices stackoverflow is super

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM