rake command not running from cron job but works otherwise - ruby-on-rails

Operating System: Ubuntu installed on a docker and a normal ubuntu installed on my personal laptop.
I am running this cron job:
*/5 * * * * /bin/bash -l -c "cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"
I have tried it with bundle exec rake spec, without /bin/bash, with whenever gem, with /usr/local/bin/rake spec, but still nothing is happening except these errors.
Errors in the log file on running the cron job:
/bin/bash: bundle: command not found
/bin/bash: rake: command not found
rake aborted!
Bundler::GemNotFound: Could not find rake-11.2.2 in any of the sources
/home/deploy/railsapp/config/boot.rb:3:in `<top (required)>'
/home/deploy/railsapp/config/application.rb:1:in `<top (required)>'
/home/deploy/railsapp/Rakefile:4:in `<top (required)>'
LoadError: cannot load such file -- bundler/setup
/home/deploy/railsapp/config/boot.rb:3:in `<top (required)>'
/home/deploy/railsapp/config/application.rb:1:in `<top (required)>'
/home/deploy/railsapp/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)
bundler: failed to load command: rake (/usr/local/bin/rake)
Bundler::GemNotFound: Could not find rake-11.2.2 in any of the sources
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:95:in `block in materialize'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:88:in `map!'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/spec_set.rb:88:in `materialize'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:140:in `specs'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:185:in `specs_for'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:174:in `requested_specs'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/environment.rb:19:in `requested_specs'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/runtime.rb:14:in `setup'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:95:in `setup'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/setup.rb:19:in `<top (required)>'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
On running rake spec manually from the command line its working fine, but from cron it is giving this error.
Any ideas ?

I am pretty sure that the issue is caused by not correctly set environment. Basically cron runs with the minimal environment so it is not aware about settings from for example .bashrc file (where you have rvm or rbenv initialization and PATH settings).
Source your environment file using . so rewrite the cron entry to something like:
*/5 * * * * /bin/bash -l -c ". /etc/environment && cd /home/deploy/railsapp && rake spec >> cron.log 2>&1"
Note that /etc/environment is just an example. You can source files like .bashrc instead.

I use whenever gem to manage crontab.
Adding
ENV.each { |k, v| env(k, v) }
to the config/schedule.rb file solved this issue for me.
Reference: https://github.com/javan/whenever/issues/656#issuecomment-239111064

Use whenever gem - this is adapter for cron, with support of rake tasks and other ruby/rails things.

Related

Errno::ENOENT: No such file or directory - osascript

I have a Rails app which i am trying to deploy in Ubuntu 18.04 LTS Linux. bundle install run ok but when I run bundle exec rake secret it shows Errno::ENOENT: No such file or directory - osascript
myappuser#ubuntu:/var/www/myapp/code$ bundle exec rake secret
rake aborted!
Errno::ENOENT: No such file or directory - osascript
/home/myappuser/.rvm/gems/ruby-2.5.0/gems/webconsole-0.2.1/lib/webconsole/lib/module.rb:95:in ``'
/home/myappuser/.rvm/gems/ruby-2.5.0/gems/webconsole-0.2.1/lib/webconsole/lib/module.rb:95:in `run_applescript'
/home/myappuser/.rvm/gems/ruby-2.5.0/gems/webconsole-0.2.1/lib/webconsole/lib/module.rb:10:in `application_exists'
/home/myappuser/.rvm/gems/ruby-2.5.0/gems/webconsole-0.2.1/lib/webconsole.rb:9:in `<top (required)>'
/var/www/myapp/code/config/application.rb:7:in `<top (required)>'
/var/www/myapp/code/Rakefile:5:in `require'
/var/www/myapp/code/Rakefile:5:in `<top (required)>'
/home/myappuser/.rvm/gems/ruby-2.5.0/bin/ruby_executable_hooks:24:in `eval'
/home/myappuser/.rvm/gems/ruby-2.5.0/bin/ruby_executable_hooks:24:in `<main>'
(See full trace by running task with --trace)
Need help.
webconsole is trying to run osascript, which is macOS-specific and not available on Linux. You can replace gem 'webconsole' in your Gemfile with the following to only use it on macOS:
gem 'webconsole' if RUBY_PLATFORM.match?(/darwin/)

Rails whenever not work with rvm

I followed this issue https://github.com/javan/whenever/issues/714
and set my schedule.rb like this.
env :PATH, ENV['PATH']
set :output, {:error => '~/Desktop/z.error.log', :standard => '~/Desktop/z.standard.log'}
every 1.minute do
runner 'Price.create'
end
Then I check my crontab -l, it showed the following command
PATH=/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails514/bin:/Users/MyUserName/.rvm/gems/ruby-2.3.3#global/bin:/Users/MyUserName/.rvm/rubies/ruby-2.3.3/bin:/Users/MyUserName/.rvm/bin:/usr/local/opt/mysql#5.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin
* * * * * /bin/bash -l -c 'cd /Users/MyUserName/Desktop/my-project && bundle exec bin/rails runner -e production '\''Price.create'\'' >> ~/Desktop/z.standard.log 2>> ~/Desktop/z.error.log'
# End Whenever generated tasks for: /Users/MyUserName/Desktop/my-project/config/schedule.rb at: 2018-01-30 02:13:32 +0800
It seems great and executable, however, I checked the error log and found the following errors.
bundler: failed to load command: bin/rails (bin/rails)
Bundler::GemNotFound: Could not find erubi-1.7.0 in any of the sources
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/spec_set.rb:88:in `block in materialize'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/spec_set.rb:82:in `map!'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/spec_set.rb:82:in `materialize'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/definition.rb:170:in `specs'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/definition.rb:237:in `specs_for'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/definition.rb:226:in `requested_specs'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/runtime.rb:108:in `block in definition_method'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/runtime.rb:20:in `setup'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler.rb:107:in `setup'
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/setup.rb:20:in `<top (required)>'
/Users/MyUserName/.rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/Users/MyUserName/.rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I felt a little bit confused and wondering why I could not load the right gem path.
Environment:
OSX: 10.13.2
Rails 5.1.4
Ruby 2.3.3
Update
I found out that the error message from log stated that
/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails4271/gems/bundler-1.16.1/lib/bundler/spec_set.rb:88:in `block in materialize'`,
However, I check my crontab -l, it clearly showed this setting
#sm start rvm
PATH="/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails514/bin:/Users/MyUserName/.rvm/gems/ruby-2.3.3#global/bin:/Users/MyUserName/.rvm/rubies/ruby-2.3.3/bin:/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails514/bin:/Users/MyUserName/.rvm/gems/ruby-2.3.3#global/bin:/Users/MyUserName/.rvm/rubies/ruby-2.3.3/bin:/Users/MyUserName/.rvm/bin:/usr/local/opt/mysql#5.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin"
GEM_HOME='/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails514'
GEM_PATH='/Users/MyUserName/.rvm/gems/ruby-2.3.3#rails514:/Users/MyUserName/.rvm/gems/ruby-2.3.3#global'
MY_RUBY_HOME='/Users/MyUserName/.rvm/rubies/ruby-2.3.3'
IRBRC='/Users/MyUserName/.rvm/rubies/ruby-2.3.3/.irbrc'
RUBY_VERSION='ruby-2.3.3'
#sm end rvm
I am confused why it still get the wrong rvm version.
Did I miss something?
Update 2
After I changed the default gemset, it works perfectly.
I used this command
rvm use 2.3.3#rails514 --default
But does anyone can tell me how to put the right path in crontab instead of setting rvm default?
Try to run
rvm cron setup
Not sure if it fix the problem, but had a similar issue once, and worked for me. Good luck!

Running Watir code in rake task

Trying to run some Watir code as a Rake task that is giving me this error:
$ bundle exec rake update_market_rents:market_rents
DL is deprecated, please use Fiddle
(in C:/Users/310046998/sites/testpropinvest)
rake aborted!
LoadError: cannot load such file -- watir-webdriver
C:/Users/310046998/sites/testpropinvest/vendor/bundle/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
C:/Users/310046998/sites/testpropinvest/vendor/bundle/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `block in require'
C:/Users/310046998/sites/testpropinvest/vendor/bundle/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
C:/Users/310046998/sites/testpropinvest/vendor/bundle/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:274:in `require'
C:/Users/310046998/sites/testpropinvest/lib/tasks/update_market_rents.rake:10:in `block (2 levels) in <top (required)>'
C:/Users/310046998/sites/testpropinvest/vendor/bundle/gems/rake-11.3.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => update_market_rents:market_rents
(See full trace by running task with --trace)
Code:
namespace :update_market_rents do
desc "TODO"
task market_rents: :environment do
#watir code
require 'watir-webdriver'
Selenium::WebDriver::Firefox::Binary.path='C:\Program Files\Mozilla Firefox\firefox.exe'
browser = Watir::Browser.new :ff
#blah blah more code
browser.close
end
end
If I run the Watir code from the command prompt using $ ruby filename.rb, I have no problems. For some reason, when I try to run it via the Rake command I get the above error. I'm struggling to find what's happening given that I see the code work okay alone.
I've tried adding require 'rubygems' but that doesn't work.
If you are on Linux then run:
sudo apt-get install nodejs
On Mac:
brew install node

Bundler::GemNotFound: Could not find nokogiri-1.5.5 in any of the sources

I am trying to run a rake task from script using crontab #reboot
The crontab looks like this:
#reboot cd /home/me/apps/packaged/X/ && ./resque_startup.sh > /home/me/sss.txt 2>&1
the resque_startup script (Yes the code can be more idiomatic but thats not the case) :
if [ -z ${1+x} ]; then
RAILS_ENV=xxx /usr/local/bin/rake deploy:resque
else
if [ $1 = 'start' ]; then
RAILS_ENV=xxx rake deploy:resque
elif [ $1 = 'stop' ]; then
echo "stop resque"
else
echo "Invalid command, use start|stop"
fi
fi
I get the following error:
rake aborted!
Bundler::GemNotFound: Could not find nokogiri-1.5.5 in any of the sources
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/spec_set.rb:92:in `block in materialize'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/spec_set.rb:85:in `map!'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/spec_set.rb:85:in `materialize'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/definition.rb:140:in `specs'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/definition.rb:185:in `specs_for'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/definition.rb:174:in `requested_specs'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/environment.rb:18:in `requested_specs'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/runtime.rb:13:in `setup'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler.rb:127:in `setup'
/var/lib/gems/1.9.1/gems/bundler-1.10.6/lib/bundler/setup.rb:18:in `<top (required)>'
/home/me/apps/packaged/X/config/boot.rb:6:in `<top (required)>'
/home/me/apps/packaged/X/config/application.rb:1:in `<top (required)>'
/home/me/apps/packaged/X/Rakefile:5:in `<top (required)>'
(See full trace by running task with --trace)
note that if i run the script manualy from X folder everything works fine
Thanks.
Try using:
bundle install --path vendor/cache

Cron Job Errors help - Rails Runner errors

* * * * * /bin/bash -l -c 'cd /Users/boris/projects/MyApp/ && rails runner "Resque.enqueue(Place)"'
Basically I need to do the following:
Load Ruby with RVM
Navigate to MyApp Dir
Run the following line: rails runner "Resque.enqueue(Place)
The above cron seems to be running, but its producing the following errors with rails runner
whats going on?
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/dependency.rb:52:in `initialize': Valid types are [:development, :runtime], not nil (ArgumentError)
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:359:in `new'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:359:in `search'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:354:in `gems_size'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:179:in `resolve'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/source_index.rb:95:in `sort_by'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:175:in `each'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:175:in `sort_by'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:175:in `resolve'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:160:in `start'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:128:in `resolve'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:127:in `catch'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/resolver.rb:127:in `resolve'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/definition.rb:151:in `resolve'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/definition.rb:90:in `specs'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/definition.rb:135:in `specs_for'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/definition.rb:124:in `requested_specs'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/environment.rb:23:in `requested_specs'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/runtime.rb:11:in `setup'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler.rb:107:in `setup'
from /Users/boris/.rvm/gems/ruby-1.9.2-p180/gems/bundler-1.0.12/lib/bundler/setup.rb:6
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `require'
from /Users/boris/projects/chaggregator/config/boot.rb:6
from script/rails:5:in `require'
from script/rails:5
Actually the best way to do this is to use rvm wrapper. You can create a wrapper like this:
rvm wrapper ruby-1.9.3-p0#somegemset appname rails
binary can be rails, rake, gem or whatever other ruby binary you have installed. What happens is that rvm creates a wrapper that will source the correct rvm environment before executing the rails command. On system wide rvm, the wrapper will normally be placed in /usr/local/rvm/bin/
Now from cron you can just do:
*/3 * * * * cd /path/to/your/app && appname_rails runner "Resque.enqueue(Place)" -e production
This will cd into your app directory and execute the rvm wrapper you just created every 3 minutes. This example is based on rails 3 and production environment.
You need to source the right environment via RVM before any Ruby code runs. So include something like this in the command:
source /usr/local/rvm/environments/ruby-1.9.2-p180#my-gemset
So a potential solution would be:
SHELL=/bin/bash
* * * * * source /{path_to_rvm_environment_for_ruby}#{gemset} && cd /Users/boris/projects/MyApp/ && rails runner "Resque.enqueue(Place)"'
Lots of moving pieces here, here are a couple of pointers on things to check in your environment (ruby, rvm, bundle) - post your findings and will go from there.
Path's issue with bundler, to change or not to change?
Bundle bug
I guess the commands runs fine outside of cron? if you're running your cron job as a different user, check the environment of that user.

Resources