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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5835675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:53:03  来源:igfitidea点击:

Setting path for whenever in cron so it can find ruby

rubybashcronshwhenever

提问by John Bachir

My ruby is in /usr/local/bin. whenevercan'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.

我的红宝石在 /usr/local/bin 中。每当找不到它,并且在我的 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

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.

我怎么知道我的 ruby​​ 二进制文件在哪里?从 /usr/bin 创建符号链接对我来说似乎很麻烦,但我想这可能是唯一的选择。

This questionoffers 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). updateactually it is in the bottom of this page, i'll try it now.

这个问题env :PATH, "..."在 schedule.rb 中提供了一个解决方案,但是 (a) 我在文档中的任何地方都找不到该功能的任何文档 (b) 它似乎没有解决提问者的问题(不幸的是它需要非平凡周转时间让我尝试一下)。 更新实际上它在这个页面的底部,我现在就试试。

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.
  1. 我无法修改 cron 命令,因为它是由任何时候生成的
  2. 我证实,如果我用bash -l,/usr/bin/env制作一个新的 bash shell发现 ruby​​ 就好了
  3. 我只是在 cron 中尝试了确切的命令,从 /bin/bash 开始,从该用户的命令行,并且它起作用了。

so, this is very mysterious...

所以,这很神秘……

回答by John Bachir

The solution is to put this in schedule.rb:

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

env :PATH, ENV['PATH']

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

这是我针对该主题整理的一份小指南。

回答by shellter

rewrite your crontab as

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

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.

或者您应该尝试弄清楚为什么您的 BASH shell 没有选择 PATH=... 这几乎可以肯定在您的 .profile 或 .bash_profile 中。

I hope this helps.

我希望这有帮助。

回答by Christian Fazzini

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.

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

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 rubycommand in a terminal.

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

$ which ruby
/usr/local/bin/ruby

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

Cron 正在使用位于/usr/bin/ruby. 这需要改变。

In schedule.rb, I have:

在 schedule.rb 中,我有:

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

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

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

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:

在临时/生产环境中,ruby 可能安装在其他地方。所以运行这个可能更合适:

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

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

您将需要替换/usr/bin/localecho $PATH.

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

然而,在 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

I hope the information here is clear enough.

我希望这里的信息足够清楚。