I am required to run the following Ubuntu bash script as "sudo" (FYI, this is because I am running it through Upstart):
cd /u/apps/MyRailsApp/current && /usr/bin/env RAILS_ENV=production script/delayed_job restart >> /var/log/upstart/delayed_job.log
However, when I run it as "sudo", I get the following error:
/var/lib/gems/1.9.1/gems/bundler-1.7.6/lib/bundler/source/git.rb:188:in rescue in load_spec_files': git#github.com:MyRemoteRepo/my_gem.git (at 1.0.1) is not yet checked out. Runbundle install` first. (Bundler::GitError)
It's not recommended to run "bundle install" as Sudo. When I run "bundle install" as a non-admin user, it is working and all the gems are recognized. How do I get the root user to recognize the gem?
The gem is in /home/ubuntu/.bundler/ruby/1.9.1/. My other gems are in /var/lib/gems/1.9.1/gems/
Thanks so much for your help.
Try adding "source /home/[username]/.bashrc" where [username] is non-admin user that can run the script successfully.
MyApp is using rvm gemset 1.9.3#rails-3.2. It is not default.
I am using gem 'whenever' to periodically send email notifications. This is my schedule.rb:
every 1.minutes do
runner "MyModel.send_emails" # if ...
end
Cron job is not working unless gemset 1.9.3#rails-3.2 is default. How can i improve my schedule.rb in order to use another gemsets (not only default #global) for my scheduller.
I have read official documentation: whenever and rvm issues and stackoverflow questions about "rvm gemsets for whenever", but have not found an answer.
I have tried to put the following code (according to advice in RVM-Notes) in my shedule.rb:
job_type :runner, "{ cd #{#current_path} > /home/####/.rvm/gems/ruby-1.9.3-p448#
rails-3.2/bin/rails; } && RAILS_ENV=:environment bundle exec rails runner ':task' :output"
but it gives no result:
ERROR:
/home/###/.rvm/gems/ruby-1.9.3-p448#global/gems/bundler-1.3.5/lib/bundler/spec_set.rb:92
:in `block in materialize': Could not find i18n-0.6.5 in any of the sources
(Bundler::GemNotFound)
How to specify rvm gemsets for Rails gem 'whenever'? Many thanks!
UPDATE
1) I have tried to provide rvm notaions for my Gemfile like this:
ruby=ruby-1.9.3-p448 # with # sign
ruby-gemset=rails-3.2 # with # sign
It gives no result. Nothing changes.
2) I have tried to modify my ~/.rvmrc. Experiments with full path to bundle, rails gives me the following list of errors:
/usr/bin/env: ruby: No such file or directory
/usr/bin/env: ruby: No such file or directory
/bin/bash: bundle: command not found
/bin/bash: bundle: command not found
/usr/bin/env: ruby_executable_hooks/usr/bin/env: ruby_executable_hooks: No such file or directory
: No such file or directory
Here by job_type experiments:
#job_type :runner, "{ cd #{path}; } && RAILS_ENV=:environment bundle exec rails runner ':task' :output"
job_type :runner, "{ cd #{path}; } && RAILS_ENV=:environment /home/###/.rvm/gems/ruby-1.9.3-p448#global/bin/bundle exec rails runner ':task' :output"
#job_type :runner, "cd #{path} && RAILS_ENV=development /home/###/.rvm/gems/ruby-1.9.3-p448#global/bin/bundle exec /home/###/.rvm/gems/ruby-1.9.3-p448#rails-3.2/bin/rails runner ':task' :output"
use wrapper:
job_type :runner, "cd #{path} && RAILS_ENV=development /home/###/.rvm/wrappers/ruby-1.9.3-p448#rails-3.2/bundle exec rails runner ':task' :output"
this will load proper rvm environment and run bundle in its context, bundler only adds environment variables for using gems from Gemfile without modifying the loaded ruby environment.
a similar situation is described here http://rvm.io/integration/init-d
try
job_type :runner, "cd :path && RAILS_ENV=:environment bundle exec rails runner :task :output"
Assuming you have mentioned in Gemfile
#ruby=ruby-1.9.3-p448
#ruby-gemset=rails-3.2
See RVM Documentation for this notation
Try to create .rvmrc file in your project root directory and have the following line
rvm use 1.9.3#rails-3.2
I am trying to use whenever to execute a rake task onces a day. Im getting this error
/bin/bash: bundle: command not found
/home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find bundler (>= 0) amongst [minitest-1.6.0, rake-0.8.7, rdoc-2.5.8] (Gem::LoadError)
from /home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
from /home/app/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems.rb:1210:in `gem'
from /home/app/.rvm/gems/ruby-1.9.2-p180/bin/bundle:18:in `<main>'
Here is my crontab
# Begin Whenever generated tasks for: /home/af/www/app/releases/20120216172204/config/schedule.rb
PATH=/home/af/.rvm/gems/ruby-1.9.2-p180#global/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
0 0 * * * /bin/bash -l -c 'cd /home/af/www/app/releases/20120216172204 && rvm 1.9.1-p180; RAILS_ENV=production /home/af/.rvm/gems/ruby-1.9.2-p180/bin/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1'
# End Whenever generated tasks for: /home/af/www/app/releases/20120216172204/config/schedule.rb
I'm at a loss as to why it isn't working. If I run the command:
cd /home/af/www/app/releases/20120216172204 && rvm 1.9.1-p180; RAILS_ENV=production /home/af/.rvm/gems/ruby-1.9.2-p180/bin/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1
It works fine, not sure whats going on here.
You can also ensure your PATH ends up in the crontab, by putting the following at the top of the schedule.rb file:
env :PATH, ENV['PATH']
https://groups.google.com/forum/#!msg/whenever-gem/yRLt3f2jrfU/Exu3xfCo8DAJ
If above solution don't work for you, try:
env :GEM_PATH, ENV['GEM_PATH']
In my case I just ran :
rvm env --path -- ruby-version[#gemset-name]
Referring to cron job setup doc
Added new source line to the command for ruby path
before bundle command in the crontab -e
source /usr/local/rvm/environments/ruby-1.9.3-p392;
Now the commands like as below:
Before:
0 4 * * * cd /home/current && bundle exec rake my_rake RAILS_ENV=production
After:
0 4 * * * cd /home/current && source /usr/local/rvm/environments/ruby-1.9.3-p392; bundle exec rake my_rake RAILS_ENV=production
Cheers!!!
After so many try outs the following seems to work
Type the following from terminal
Type crontab -e
This opens the crontab for editing. You will see two lines as below:
# cron clears out environment variables, but Rubber.root/script/rubber uses
# "rvm do default" to run, so no longer any need to setup ruby env vars here,
# all we need is PATH
PATH=/<path to bundle>/bundle/ruby/1.9.1/bin:/usr/local/rvm/gems
AND
# Begin Whenever generated tasks for: /mnt/wamjoke-production/releases/20120912$
PATH=/<path to bundle>/shared/bundle/ruby/1.9.1/bin:/usr/local/rvm/gems
Comment out both lines beginning with PATH.
Do the above step whenever you run "bundle exec whenever" command. And it works.
No idea why PATH is misleading the environment.
I hate this problem - I've spent hours trying to solve it too.
What works for me is to add
RAILS_ENV=production; source /usr/local/rvm/scripts/rvm;
before the bundle command.
Forget about PATH settings in cron files. Setting the PATH does not work.
Set the path to bundle explicitly in your config/schedule.rb
set :bundle_command, "/usr/local/bin/bundle"
You can try below solution which I found while googling and that works for me finally....hope that should work with you.
I implemented and tested the same on production make sure that to change environment accordingly -
set :output, "{your path on the server}/log/cron_log.log"
set :environment, :production
env :PATH, ENV['PATH']
job_type :rbenv_rake, %q!eval "$(rbenv init -)"; cd :path && :environment_variable=:environment bundle exec rake :task --silent :output!
Best luck, This issue occurred after 3 years as I was using before just simple what given on the gem documentation on production.
I'm using Ruby 2.x and Rails 4.2 with whenever 0.9.4 latest version. It should work with earlier version as well, if the nature of the issue is same.
thank you.
I think you should try explicitly setting the GEM_HOME and GEM_PATH environment variables in your crontab. You could also try running something like gem list --local or gem environment through cron and checking the output.
I played around with this all afternoon and couldn't find a better solution. Here is what I have come up with
bundle install --binstubs
and then run
bin/rake daily:stats
By executing a command that way: /bin/bash -l -c
You are launching a bash command as a login shell which is going to source (execute) /etc/profile bash file as a setup file. By doing so, if you check this file, it might have bash command lines that erase your previous $PATH which you do not want to since it contains your path to your bundle and all your other commands in the first place.
To fix this issue you just have to remove the lines related to set up the $PATH variable in your /etc/profile file.
This is a ENV['PATH'] not set issue. The most elegant way to fix this is to append the rvm related scripts to the path right after the install. Add the following lines to beginning of .bashrc ( beginning and not end as when .bashrc is accessed by a non-interactive shell, the line [ -z "$PS1" ] && return throws error and the subsequent lines are not executed.
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
and not try to explicitly set PATH and sully environment variables.
For those using rbenv you can use the included shim /home/username/.rbenv/shims/bundle
0 0 * * * /bin/bash -l -c 'cd /home/af/www/app/releases/20120216172204 && RAILS_ENV=production /home/af/.rbenv/shims/bundle exec rake daily:stats --silent >> /home/af/www/app/releases/20120216172204/log/cron.log 2>&1'
in 2021, I found a basic solution, just add on top of schedule.rb
env :PATH, ENV['PATH']
set :output, "log/cron_log.log"
set :runner_command, "rails runner"
from:
https://github.com/javan/whenever/issues/665
I solved this problem by printing out my environmental variables
printenv
finding the ones that look related to Rails. One was a path to gems, the other was GEM_HOME and prepending the command in cron with these two:
PATH=$PATH:/home/petr/gems/bin GEM_HOME=/home/petr/gems program_executable
Also in 2021, adding this in schedule.rb worked for me:
set :job_template, "bash -l -c 'PATH=#{ENV['PATH']} && :job'"
All jobs are by default run with bash -l -c 'command...' (https://github.com/javan/whenever)
So I made bash include ENV['PATH'] in PATH at the beginning and now rails are called from the proper rbenv.
For modern fix, add this line in capistrano deploy.rb,
set :whenever_command, "bundle exec whenever"
[root#smbserver current]# crontab -e
02 22 * * 1-5 /bin/bash -l -c /shell/day.sh
30 14 * * 0 /bin/bash -l -c /shell/week.sh
Everything was working fine before upgrading but now I can't run rake tasks.. any idea if this is something to do with rake 0.9.2.2?
I don't understand why this problem sometimes occurs.
[gh (master)]$ bundle list | grep rake
* rake (0.9.2.2)
[gh (master)]$ gem list | grep rake
rake (0.9.2.2, 0.8.7)
I'll take an educated guess and say that rails 3.2 requires a different version of rake from rails 3.1
If you run bundle exec rake instead of just rake then it will use the version specified in your current Gemfile.lock
If that doesn't work, please post the contents of :
bundle list | grep rake
And
gem list | grep rake
Two ways.. I preferred the
1st way:
I typed this from home directory in terminal:
nano .bash_profile
and pasted this in the file:
bundle_commands="rake spec rspec cucumber cap watchr rails rackup"
function run_bundler_cmd () {
if [ -r ./Gemfile ]; then
bundle exec $#
else
$#
fi
}
for cmd in $bundle_commands
do
alias $cmd="run_bundler_cmd $cmd"
done
then typed:
source ~/.bash_profile or restart terminal
2nd way.
I typed this from home directory in terminal:
nano .bashrc <br />
nano .bash_profile <br />
copied and pasted this into the files:
# fix for ruby on rails rake issue
alias bi="bundle install --binstubs=.bin"
export PATH="./.bin:$PATH"
then closed terminal and re-opened to get everything working.
I also added .bin to my gitignore file.
Install Gem from Github Branch?
I have been following the guide from that thread. When I get to step 4, rake gem , I get an error
rake aborted!
Don't know how to build task 'gem'
Looks like they are using Jeweler which has a rake build task to build the gem.
You can check what rake tasks are available by doing rake -T or rake -vT (more verbose). If I do that in a gem's root directory that uses Jeweler I get:
rake build # Build gem
as an option.
I think you can also skip the manual install step by doing rake install too.
Don't forget to install jeweler:
sudo gem install technicalpickles-jeweler -s http://gems.github.com
Otherwise it won't build.