在 cron 中设置路径,以便它可以找到 ruby
我的 ruby 位于 /usr/local/bin 中。 whenever 找不到它,并且在我的 cron 文件顶部设置 PATH 不起作用要么,我认为是因为每当在新的 bash 实例中运行命令时。
# 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
我怎样才能知道我的 ruby 二进制文件在哪里?从 /usr/bin 创建符号链接对我来说似乎很混乱,但我想这可能是唯一的选择。
这个问题提供env :PATH, “...”在schedule.rb中作为解决方案,但是(a)我在文档中的任何地方都找不到该功能的任何文档(b)它似乎没有解决提问者的问题(很遗憾我需要花费不小的周转时间来尝试)。 更新实际上它位于此页面的底部< /a>,我现在就试试。
更多信息
- 我无法修改 cron 命令,因为每当
- 我验证是否使用
bash -l
, /usr/bin/env 创建一个新的 bash shell 时 都会生成它发现 ruby 很好, - 我刚刚在 cron 中尝试了确切的命令,从 /bin/bash 开始,从该用户的命令行,它起作用了。
所以,这是非常神秘的......
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
- I can't modify the cron command because it's generated by whenever
- i verified that if I make a new bash shell with
bash -l
, /usr/bin/env finds ruby just fine - 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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案是将其放入
schedule.rb
中:这是我整理的关于主题。
The solution is to put this in
schedule.rb
:Here's a little guide I put together on the topic.
将你的 crontab 重写为
或者你应该尝试找出为什么你的 BASH shell 没有选择几乎肯定在你的 .profile 或 .bash_profile 中的 PATH=... 。
我希望这有帮助。
rewrite your crontab as
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.
正如 John Bachir 指出的,您可以通过
env
来完成。但让我添加更多的意见。我正在 AWS Opsworks 上部署。不幸的是,他们默认没有安装 ruby 管理器(RVM、Rbenv 等)。我需要做的第一件事是通过 SSH 进入实例并找出我正在使用哪个 ruby。通过在终端中执行
which ruby
命令,这很容易。Cron 使用位于
/usr/bin/ruby
的 ruby。这需要改变。在schedule.rb中,我有:
在本地,不需要设置
env_path
。对于大多数用户来说,唯一要做的就是在任何时候执行:在临时/生产环境中,ruby 可能安装在其他地方。因此,运行此命令可能更合适:
您需要将 /usr/bin/local 替换为
echo $PATH
的输出。然而,在 Opsworks 中,我需要创建一个自定义 Chef 食谱,如下所示:
我希望此处的信息足够清晰。
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.Cron was using ruby located at
/usr/bin/ruby
. This needed to be changed.In schedule.rb, I have:
In local,
env_path
doesn't need to be set. For most users, the only thing to do is execute whenever as such:On a staging / production environment, ruby may be installed elsewhere. So running this may be more appropriate:
You will need to replace
/usr/bin/local
with the output ofecho $PATH
.In Opsworks, however, I needed to create a custom Chef recipe that looked like:
I hope the information here is clear enough.