开发者

Setting path for whenever in cron so it can find ruby

开发者 https://www.devze.com 2023-03-02 04:49 出处:网络
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 inside of a new

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 inside of a new bash instance.

# this does not work
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin

# Begin Whenever generated tasks for: foo
0 * * * * /bin/bash -l -c 'cd /srv/foo/releases/20110429110637 && script/rails runner -e production '\''ActiveRecord::SessionStore::Session.destroy_recent(15)'\'''

# End Whenever generated tasks for: foo

How can I tell whenever where my ruby binary is? Making a symbolic link from /usr/bin seems messy to me, but I guess that might be the only option.

This question offers env :PATH, "..." in schedule.rb as a solution, but (a) I can't find any documentation of that feature anywhere in the docs (b) it doesn't seem to have solved the asker's problem (unfortunately it takes non-trivial turnaround time for me to just try it). update actually it is in the bottom of this page, i'll try it now.

more info

  1. I can't modify the cron command because it's generated by whenever
  2. i verified that if I make a new bash shell with bash -l, /usr/bin/env finds ruby just fine
  3. I just tried the exact command in cron, starting with /bin/bash, from the command line of that user, and it worked.

so, this is very mysterious...


The solution is to put this in schedule.rb:

env :PATH, ENV['PATH']

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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号