问题
I have install ruby by rvm (system-wide), and worked correctly via normal console and my rails program is behaving correctly with both rails runner
and apache2+passenger
.
Now in a crontab, I called rails runner foo.bar
, it gives up, carefully examine the log i see that:
/usr/bin/env: ruby: No such file or directory
Anyone knows why /usr/bin/env doesnt work in crontab?
回答1:
If you installed ruby via rvm, ruby probably isn't in /usr/bin. Depending on where rvm is installed:
bash -c "source /usr/local/lib/rvm" && rails runner foo.bar
You probably added a source */rvm to your bashrc that is the correct rvm loading script.
回答2:
your cron isn't inheriting your environment. try echoing "$PATH" to a file to see what it's set to.
You could also just do "PATH=/usr/bin/ruby && foo.rb"
回答3:
I had a similar problem. Cron seems to run commands by default without using the PATH settings you would expect for the user (when logging in as that user). In fact it didn't even seem to use any of the default PATH settings (be it in /etc/profile or elsewhere).
I was able to find the problem using the following commands (the first one is how cron seems to run commands):
su -c 'printenv PATH' userX
With output: /usr/local/bin:/usr/bin:/bin:/usr/games
su -l userX -c 'printenv PATH'
With output: /opt/ruby-enterprise-1.8.7-2010.02/bin/:/opt/ruby-enterprise-1.8.7-2010.02/bin/:/usr/local/bin:/usr/bin:/bin:/usr/games
The first command doesn't seem to populate the PATH variable in any way except for the bare system default. In my case I solved it by just adding the necessary (REE) path to: /etc/login.defs, which by default looks as follows:
/etc/login.defs:103:ENV_PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/games
来源:https://stackoverflow.com/questions/5241572/why-usr-bin-env-ruby-doesnt-work-in-crontab