为什么#!/ usr / bin / env ruby​​在crontab中不起作用?

时间:2022-09-14 08:00:07

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.

我已经通过rvm(系统范围)安装ruby,并通过普通控制台正常工作,我的rails程序与rails runner和apache2 + passenger正常运行。

Now in a crontab, I called rails runner foo.bar, it gives up, carefully examine the log i see that:

现在在crontab中,我调用了rails runner foo.bar,它放弃了,仔细检查日志我看到:

/usr/bin/env: ruby: No such file or directory

Anyone knows why /usr/bin/env doesnt work in crontab?

任何人都知道为什么/ usr / bin / env在crontab中不起作用?

3 个解决方案

#1


7  

If you installed ruby via rvm, ruby probably isn't in /usr/bin. Depending on where rvm is installed:

如果你通过rvm安装了ruby,ruby可能不在/ usr / bin中。根据rvm的安装位置:

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.

你可能在你的bashrc中添加了一个源* / rvm,它是正确的rvm加载脚本。

#2


4  

your cron isn't inheriting your environment. try echoing "$PATH" to a file to see what it's set to.

你的cron没有继承你的环境。尝试将“$ PATH”回显到文件以查看它的设置。

You could also just do "PATH=/usr/bin/ruby && foo.rb"

你也可以做“PATH = / usr / bin / ruby​​ && foo.rb”

#3


0  

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

我有类似的问题。 Cron似乎默认运行命令而不使用您期望的用户PATH设置(以该用户身份登录时)。实际上它似乎甚至没有使用任何默认的PATH设置(无论是在/ etc / profile还是其他地方)。

I was able to find the problem using the following commands (the first one is how cron seems to run commands):

我能够使用以下命令找到问题(第一个是cron如何运行命令):

su -c 'printenv PATH' userX

su -c'printenv PATH'userX

With output: /usr/local/bin:/usr/bin:/bin:/usr/games

输出:/ usr / local / bin:/ usr / bin:/ bin:/ usr / games

su -l userX -c 'printenv PATH'

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

输出:/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 /游戏

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:

除了裸系统默认值之外,第一个命令似乎没有以任何方式填充PATH变量。在我的情况下,我通过添加必要的(REE)路径来解决它:/etc/login.defs,默认情况下如下所示:

/etc/login.defs:103:ENV_PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/games

#1


7  

If you installed ruby via rvm, ruby probably isn't in /usr/bin. Depending on where rvm is installed:

如果你通过rvm安装了ruby,ruby可能不在/ usr / bin中。根据rvm的安装位置:

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.

你可能在你的bashrc中添加了一个源* / rvm,它是正确的rvm加载脚本。

#2


4  

your cron isn't inheriting your environment. try echoing "$PATH" to a file to see what it's set to.

你的cron没有继承你的环境。尝试将“$ PATH”回显到文件以查看它的设置。

You could also just do "PATH=/usr/bin/ruby && foo.rb"

你也可以做“PATH = / usr / bin / ruby​​ && foo.rb”

#3


0  

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

我有类似的问题。 Cron似乎默认运行命令而不使用您期望的用户PATH设置(以该用户身份登录时)。实际上它似乎甚至没有使用任何默认的PATH设置(无论是在/ etc / profile还是其他地方)。

I was able to find the problem using the following commands (the first one is how cron seems to run commands):

我能够使用以下命令找到问题(第一个是cron如何运行命令):

su -c 'printenv PATH' userX

su -c'printenv PATH'userX

With output: /usr/local/bin:/usr/bin:/bin:/usr/games

输出:/ usr / local / bin:/ usr / bin:/ bin:/ usr / games

su -l userX -c 'printenv PATH'

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

输出:/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 /游戏

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:

除了裸系统默认值之外,第一个命令似乎没有以任何方式填充PATH变量。在我的情况下,我通过添加必要的(REE)路径来解决它:/etc/login.defs,默认情况下如下所示:

/etc/login.defs:103:ENV_PATH PATH=/usr/local/bin:/usr/bin:/bin:/usr/games