Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting path for whenever in cron so it can find ruby

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

like image 884
John Bachir Avatar asked Apr 29 '11 18:04

John Bachir


People also ask

What path does cron use?

The cron daemon automatically sets several environment variables . The default path is set to PATH=/usr/bin:/bin . If the command you are executing is not present in the cron specified path, you can either use the absolute path to the command or change the cron $PATH variable.

Does crontab require full path?

Generally speaking it's considered good practice to always use the full path in scripts to be run from cron, as cron may well have a different setting for PATH to you. The alternative is to call your script from cron as /full/location/of/script , and set a new value for PATH in the script.


2 Answers

The solution is to put this in schedule.rb:

env :PATH, ENV['PATH']

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

like image 171
John Bachir Avatar answered Oct 17 '22 10:10

John Bachir


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.

like image 34
shellter Avatar answered Oct 17 '22 12:10

shellter