我试图使用cron作业运行ruby代码,并使用whenever gem。
以下是.rb文件中的简单代码
require "watir"
require "selenium-webdriver"
browser = Watir::Browser.start('https://www.google.com',:firefox)
sleep 5
browser.close我的crontab -l输出是:
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 +0400this显示了以下内容:
/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>'当我试图用rails运行cron作业时,也会出现同样的错误。
代码在终端上运行良好。*问题只是当我尝试使用cron作业运行它时!*
而且,我也试着不用任何时候的宝石。即使是新鲜的ubuntu 16,红宝石和铁轨。
发布于 2018-04-13 06:35:34
cron在执行时丢失了显示环境变量。
将ENV‘’DISPLAY‘= ":0“添加到我的脚本中,修复了我的情况
详情:
新日志:
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'我看到了:错误:没有显示环境变量指定的
非常感谢你的好建议,堆叠溢出是超级棒
https://stackoverflow.com/questions/49797513
复制相似问题