Rails cron whenever, bundle: command not found
Asked Answered
T

18

54

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.

Terle answered 28/2, 2012 at 12:55 Comment(2)
If you don't get an answer to this, please bug me and I'll tell you what works for me. I'd love for somebody else to answer this question though, as I've never liked my solution.Deceitful
I've been trying to solve this for the last couple of weeks. Really at a loss as to what is causing it. What is your solution?Terle
T
1

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
Terle answered 28/2, 2012 at 17:51 Comment(1)
Did you try Dennis's suggestion of adding env :PATH, ENV['PATH'] to your schedule.rb? That worked for me.Paganini
B
71

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']
Benefice answered 15/6, 2012 at 20:11 Comment(4)
Just in case. This worked for me but then coudln't find my gemfiles. I fixed it with env :GEM_PATH, ENV['GEM_PATH']Sarsenet
This did not work for me - could you try removing the env :PATH, ENV['PATH'] and confirm that this is actually a fix?Proceeding
This env :GEM_PATH, ENV['GEM_PATH'] works for me. ThanksHearsay
This worked for me even more than 10 years later...Antennule
L
6

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!!!

Lord answered 22/10, 2015 at 1:32 Comment(0)
A
4

After so many try outs the following seems to work

Type the following from terminal

  1. 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
    
  2. 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.

Apposition answered 12/9, 2012 at 11:17 Comment(0)
D
3

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.

Deceitful answered 28/2, 2012 at 18:20 Comment(1)
I think I've got it. When you're not an RVM user there's no problem, But If you're, you better stick to these guidelines: rvm.io/integration/cron So, By using the RVM generated wrappers I managed to come with a clean syntax to place in crontab that actually works... 1 0 * * * cd ~/Desktop/cellar/ && /home/jose/.rvm/bin/rake-ruby-1.9.3-p0 wines:destroy_from_1969 >> ~/Desktop/cron_log.log 2>&1 that's for a Rails app located at ~/Desktop/cellar/Synchro
P
3

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.

Powered answered 16/3, 2016 at 13:52 Comment(0)
P
2

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"
Proceeding answered 28/12, 2014 at 18:38 Comment(0)
A
1

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.

Anastasia answered 28/2, 2012 at 15:57 Comment(0)
T
1

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
Terle answered 28/2, 2012 at 17:51 Comment(1)
Did you try Dennis's suggestion of adding env :PATH, ENV['PATH'] to your schedule.rb? That worked for me.Paganini
J
1

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.

Junji answered 15/1, 2018 at 5:53 Comment(0)
C
1

Add to your schedule.rb

set :bundle_command, '~/.rbenv/shims/bundle exec'

if you use rbenv. In case of rvm or smth else simply change path to your bundle binary:

$ which bundle

See also how defined this and other variables

Cesya answered 8/12, 2023 at 8:52 Comment(0)
G
0

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.

Glance answered 25/7, 2013 at 12:31 Comment(0)
L
0

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'
Landis answered 18/10, 2020 at 9:46 Comment(0)
D
0

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

Datnow answered 11/1, 2021 at 12:47 Comment(0)
B
0

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
Baseball answered 3/2, 2021 at 5:14 Comment(0)
T
0

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.

Tao answered 24/3, 2021 at 22:24 Comment(0)
F
0

2023, still having this issue. What worked for me in development. Adjust these to production paths/settings.

set :bundle_command, "/Users/$username/.rbenv/shims/bundler exec"
set :environment, :development
set :output, {:error => "log/cron_error_log.log", :standard => "log/cron_log.log"}
Fibrinolysis answered 24/7, 2023 at 11:10 Comment(1)
The issue is about bundle being accessible meaning PATH variable including it. The solution lies within that instead of setting a custom bundle command. Something needs to be sourced.Varicolored
K
-1

For modern fix, add this line in capistrano deploy.rb,

set :whenever_command, "bundle exec whenever"
Korfonta answered 8/8, 2013 at 8:41 Comment(1)
Instead, set the :bundle_command, "/usr/local/bin/bundle" per my answer. Forget about trying to get the PATH to work.Proceeding
S
-5

[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 
Sfumato answered 11/7, 2013 at 4:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.