why #!/usr/bin/env ruby doesn't work in crontab?

六眼飞鱼酱① 提交于 2019-12-04 00:54:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!