Rails cron whenever, bundle: command not found

≡放荡痞女 提交于 2019-11-27 18:07:09
Dennis Kuczynski

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']

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.

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

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.

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

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"

axzwl

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.

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.

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 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!