OS: Mac OS X 10.10.3; Ruby version: 2.2.0;
I have a problem with using cron, that launches ruby script. Problem: cron launches ruby script, but nothing happened. That cron says in its logs (script requires mongo gem) :
/Users/username/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- mongo (LoadError)
from /Users/username/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/username/project_path/main.rb:5:in `<main>'
Cron settings:
SHELL = /bin/bash
MAILTO = username
* * * * * /Users/username/.rvm/rubies/default/bin/ruby /Users/username/project_path/main.rb
So ruby can't load gem, while the script was launched by cron, but when I launch it from the IDE or terminal, it works without problems
This is a common problem. Basically the environment in which your cron
job is running is different from the environment in your IDE / terminal.
You need to investigate solutions along these lines . Understanding what's going on will involve learning more about cron, users, and other related topics.
Ok. After a couple of hours I finally solved this problem. And the solution is very easy. I don't know, why it's working only that way. So, the solution:
1.Create bash script with this content:
#!/usr/bin/env bash
/Users/username/.rvm/gems/ruby-2.2.0/wrappers/ruby /path/to/your/ruby_script.rb
2. Configure cron file:
SHELL = /bin/bash
* * * * * /path/to/created/at/first/step/bash_script.sh
That's all! It's working for me!
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.