每当作为 cron 执行的 rake 任务中使用错误的 ruby​​ 路径

发布于 2024-10-08 01:50:04 字数 954 浏览 7 评论 0原文

我正在尝试使用 whenever gem 来执行几个 rake 任务。它似乎正确设置了 cron 任务 - 这里的信息来自生成的邮件输出。以下是执行命令的示例:

/bin/bash -l -c 'cd /path/to/deployed/app && RAILS_ENV=production

rake clean:my:task --silent'

以下是一些环境变量:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>

以下是随后出现的错误:

/bin/bash: /usr/bin/rake: /usr/bin/ruby: bad interpreter: No such file or directory

当我登录时,我可以从相同的环境运行这些 rake 任务目录中,但是当我运行时,

which ruby

我得到

/usr/local/bin/ruby

There does suggest to be a 'rake' in /usr/bin, but running

/usr/bin/rake -T

给了我同样的错误:

-bash: /usr/bin/rake: /usr/bin/ruby: bad interpreter: No such file or directory

解决这个问题的最佳方法是什么?

I am trying to use the whenever gem to execute a couple of rake tasks. It appears to set up the cron tasks correctly - info here is from the mail output that is produced. Here's an example of the command executed:

/bin/bash -l -c 'cd /path/to/deployed/app && RAILS_ENV=production

rake clean:my:task --silent'

And here are some of the environment variables:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>

And here is the error that follows:

/bin/bash: /usr/bin/rake: /usr/bin/ruby: bad interpreter: No such file or directory

When i'm logged in i can run these rake tasks from the same directory, but when i run

which ruby

i get

/usr/local/bin/ruby

There does appear to be a 'rake' in /usr/bin, but running

/usr/bin/rake -T

gives me the same error:

-bash: /usr/bin/rake: /usr/bin/ruby: bad interpreter: No such file or directory

What would be the best way to resolve this?

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

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

发布评论

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

评论(4

友谊不毕业 2024-10-15 01:50:04

/usr/local/bin/ 在你的路径中吗?当你运行哪个 ruby​​ 时,你得到了 /usr/local/bin/ruby 但当你运行 rake 时,它​​正在寻找 /usr/bin/ruby

或者你可以像这样符号链接 ruby​​:

sudo ln -s /usr/local/bin/ruby / usr/bin/.

Is /usr/local/bin/ in your path? When you ran which ruby you got /usr/local/bin/ruby but when you run rake it's looking for /usr/bin/ruby

Or you could just symlink ruby like:

sudo ln -s /usr/local/bin/ruby /usr/bin/.

三生路 2024-10-15 01:50:04

这是我现在使用的:

every 1.day, :at => '5am' do
  # It appears that, when the following tasks are executed through
  #  cron, /usr/local/bin is not in the path (which is the ruby/RoR installation that should be used)
  #  So we need to make sure that the proper ruby/RoR installation can be found
  #  (Unfortunately this seems to restrict us to using the 'command' option, and leaves us unable
  #  to use the other options: 'runner' and 'rake' because i'm not sure how you'd affect the PATH
  #  environment variable for those)

  # (alot of what's below is just meant to provide a bit of visibility to the execution within cron)
  cmd_root = "PATH=/usr/local/bin:$PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "export PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "cd #{path}"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "pwd "
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "echo $PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "echo $RAILS_ENV"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "which ruby"

  # Execute the rake task, using 'command'
  cmd = cmd_root + " && rake clean:my:task param=val"
  command cmd

  # Without another way of manipulating the environment PATH, this isn't working right:
  #rake "clean:my:task"
end

由于我只是使用“命令”选项,它允许我在实际的 rake 任务之前执行命令,例如将正确的目录添加到 PATH 以便使用正确的 ruby​​。

我并不热衷于如何硬编码对 /usr/local/bin 的引用,但是:-/。我不知道如何随时使用“耙子”或“跑步者”选项。但它似乎正在发挥作用。

Here's what i'm using now:

every 1.day, :at => '5am' do
  # It appears that, when the following tasks are executed through
  #  cron, /usr/local/bin is not in the path (which is the ruby/RoR installation that should be used)
  #  So we need to make sure that the proper ruby/RoR installation can be found
  #  (Unfortunately this seems to restrict us to using the 'command' option, and leaves us unable
  #  to use the other options: 'runner' and 'rake' because i'm not sure how you'd affect the PATH
  #  environment variable for those)

  # (alot of what's below is just meant to provide a bit of visibility to the execution within cron)
  cmd_root = "PATH=/usr/local/bin:$PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "export PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "cd #{path}"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "pwd "
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "echo $PATH"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "echo $RAILS_ENV"
  cmd_root = cmd_root + " && "
  cmd_root = cmd_root + "which ruby"

  # Execute the rake task, using 'command'
  cmd = cmd_root + " && rake clean:my:task param=val"
  command cmd

  # Without another way of manipulating the environment PATH, this isn't working right:
  #rake "clean:my:task"
end

Since i'm just using the 'command' option, it allows me to execute commands prior to the actual rake task, like adding the proper directory to the PATH so that the right ruby is used.

I'm not crazy about how this hard-codes a reference to /usr/local/bin, tho :-/. And i don't know how i could utilize the 'rake' or 'runner' options of whenever. But it seems to be working.

浅语花开 2024-10-15 01:50:04

我与 rakes 版本发生冲突...我首先使用 apt-get 安装它,然后使用 gem 安装它...所以我删除了每个版本...我什至必须手动删除 /usr/local/bin/rake 。仅使用 gem 重新安装,现在它可以工作了:)希望它可以帮助别人。

I had a conflict with rakes versions... I had installed it first with apt-get and then with gem... so I removed every version... I even had to remove /usr/local/bin/rake manually. Reinstalled just with gem and now it works :) Hope it helps someone.

弱骨蛰伏 2024-10-15 01:50:04

config/schedule.rb 中添加以下代码行以设置正确的 ruby​​ 路径。

 env :PATH, ENV['PATH']

然后运行以下命令

whenever --update-crontab     #   to update crontab
service crond restart         #   to restart crontab

Add bellow line of code in config/schedule.rb to set correct ruby path.

 env :PATH, ENV['PATH']

and then run bellow commands

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