Setting path for whenever in cron so it can find ruby
Asked Answered
M

3

19

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

Moisesmoishe answered 29/4, 2011 at 18:9 Comment(1)
you say you have ruby in /usr/local/bin, but you don't include it in your PATH? adding a :/usr/local/bin should work (i checked a cron file of mine and its exactly the same)Costanza
M
28

The solution is to put this in schedule.rb:

env :PATH, ENV['PATH']

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

Moisesmoishe answered 4/5, 2011 at 5:30 Comment(4)
do you have any other input? I added this and i get the same error in cron.log, and i'm sure my path is right. i can even run /usr/local/bin/ruby -v and get the proper responseFuddle
Well, this doesn't seem to work anymore with the more recent whenever that I have??Flori
If you want you can trim this down to "env :PATH, ENV['PATH']" to set it automatically rather than hard coded.Argufy
For some reason this method didn't work for me so I ended up adding all environments instead of only the path ENV.each { |k, v| env(k, v) }Zellers
U
2

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.

Ulda answered 29/4, 2011 at 18:12 Comment(1)
thanks, added some more info to my question based on your responseMoisesmoishe
K
2

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.

Keratosis answered 4/2, 2014 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.