在 cron 中设置路径,以便它可以找到 ruby

发布于 2024-11-04 07:23:28 字数 1238 浏览 6 评论 0原文

我的 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>,我现在就试试。

更多信息

  1. 我无法修改 cron 命令,因为每当
  2. 我验证是否使用 bash -l, /usr/bin/env 创建一个新的 bash shell 时 都会生成它发现 ruby​​ 很好,
  3. 我刚刚在 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2024-11-11 07:23:28

解决方案是将其放入 schedule.rb 中:

env :PATH, ENV['PATH']

这是我整理的关于主题。

The solution is to put this in schedule.rb:

env :PATH, ENV['PATH']

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

想挽留 2024-11-11 07:23:28

将你的 crontab 重写为

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)'\''' ; }

或者你应该尝试找出为什么你的 BASH shell 没有选择几乎肯定在你的 .profile 或 .bash_profile 中的 PATH=... 。

我希望这有帮助。

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.

十年九夏 2024-11-11 07:23:28

正如 John Bachir 指出的,您可以通过 env 来完成。但让我添加更多的意见。我正在 AWS Opsworks 上部署。不幸的是,他们默认没有安装 ruby​​ 管理器(RVM、Rbenv 等)。

我需要做的第一件事是通过 SSH 进入实例并找出我正在使用哪个 ruby​​。通过在终端中执行 which ruby​​ 命令,这很容易。

$ which ruby
/usr/local/bin/ruby

Cron 使用位于 /usr/bin/ruby 的 ruby​​。这需要改变。

在schedule.rb中,我有:

set :env_path, ''
env :PATH, @env_path if @env_path.present?

在本地,不需要设置env_path。对于大多数用户来说,唯一要做的就是在任何时候执行:

bundle exec whenever --set 'environment=development' --update-crontab

在临时/生产环境中,ruby 可能安装在其他地方。因此,运行此命令可能更合适:

bundle exec whenever --set 'environment=staging&env_path=/usr/bin/local' --update-crontab

您需要将 /usr/bin/local 替换为 echo $PATH 的输出。

然而,在 Opsworks 中,我需要创建一个自定义 Chef 食谱,如下所示:

node[:deploy].each do |application, deploy|
  execute 'whenever' do
    user 'deploy'
    group 'nginx'
    cwd "#{deploy[:deploy_to]}/current"
    command "bundle exec whenever --set 'environment=#{deploy[:environment_variables][:RAILS_ENV]}&env_path=#{ENV['PATH']}' --update-crontab"
  end
end

我希望此处的信息足够清晰。

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.

$ which ruby
/usr/local/bin/ruby

Cron was using ruby located at /usr/bin/ruby. This needed to be changed.

In schedule.rb, I have:

set :env_path, ''
env :PATH, @env_path if @env_path.present?

In local, env_path doesn't need to be set. For most users, the only thing to do is execute whenever as such:

bundle exec whenever --set 'environment=development' --update-crontab

On a staging / production environment, ruby may be installed elsewhere. So running this may be more appropriate:

bundle exec whenever --set 'environment=staging&env_path=/usr/bin/local' --update-crontab

You will need to replace /usr/bin/local with the output of echo $PATH.

In Opsworks, however, I needed to create a custom Chef recipe that looked like:

node[:deploy].each do |application, deploy|
  execute 'whenever' do
    user 'deploy'
    group 'nginx'
    cwd "#{deploy[:deploy_to]}/current"
    command "bundle exec whenever --set 'environment=#{deploy[:environment_variables][:RAILS_ENV]}&env_path=#{ENV['PATH']}' --update-crontab"
  end
end

I hope the information here is clear enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文