Setting path for whenever in cron so it can find ruby

后端 未结 3 1870
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 03:07

My ruby is in /usr/local/bin. whenever can\'t find it, and setting PATH at the top of my cron file doesn\'t work either, I think because whenever is running the command insi

相关标签:
3条回答
  • 2020-12-14 03:11

    The solution is to put this in schedule.rb:

    env :PATH, ENV['PATH']
    

    Here's a little guide I put together on the topic.

    0 讨论(0)
  • 2020-12-14 03:17

    As John Bachir pointed out, you can do it via env. But let me add more input. I am deploying on AWS Opsworks. Unfortunately they do not have a ruby manager (RVM, Rbenv, etc) installed by default.

    The first thing I needed to do was SSH into the instance and figure out which ruby I was using. This was easy enough by executing the which ruby command in a terminal.

    $ which ruby
    /usr/local/bin/ruby
    

    Cron was using ruby located at /usr/bin/ruby. This needed to be changed.

    In schedule.rb, I have:

    set :env_path, ''
    env :PATH, @env_path if @env_path.present?
    

    In local, env_path doesn't need to be set. For most users, the only thing to do is execute whenever as such:

    bundle exec whenever --set 'environment=development' --update-crontab
    

    On a staging / production environment, ruby may be installed elsewhere. So running this may be more appropriate:

    bundle exec whenever --set 'environment=staging&env_path=/usr/bin/local' --update-crontab
    

    You will need to replace /usr/bin/local with the output of echo $PATH.

    In Opsworks, however, I needed to create a custom Chef recipe that looked like:

    node[:deploy].each do |application, deploy|
      execute 'whenever' do
        user 'deploy'
        group 'nginx'
        cwd "#{deploy[:deploy_to]}/current"
        command "bundle exec whenever --set 'environment=#{deploy[:environment_variables][:RAILS_ENV]}&env_path=#{ENV['PATH']}' --update-crontab"
      end
    end
    

    I hope the information here is clear enough.

    0 讨论(0)
  • 2020-12-14 03:24

    rewrite your crontab as

    0 * * * * { PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin ; export PATH ;/bin/bash -l -c 'cd /srv/foo/releases/20110429110637 && script/rails runner -e production '\''ActiveRecord::SessionStore::Session.destroy_recent(15)'\''' ; }
    

    Or you should try to figure out why your BASH shell is not picking the PATH=... that is almost certainly in your .profile or .bash_profile.

    I hope this helps.

    0 讨论(0)
提交回复
热议问题