需要在每个 cron 作业之前设置 rvm 环境 [英] Need to set up rvm environment prior to every cron job

查看:18
本文介绍了需要在每个 cron 作业之前设置 rvm 环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我大致按照这组说明的第一部分中概述的模式安装和配置了 RVM:http://blog.ninjahideout.com/posts/a-guide-to-a-nginx-passenger-and-rvm-server

I installed and configured RVM roughly following the pattern outlined in the first part of this set of instructions: http://blog.ninjahideout.com/posts/a-guide-to-a-nginx-passenger-and-rvm-server

基本上,这意味着没有预构建系统 ruby​​(所有 ruby​​ 安装都是由 RVM 管理的)并且 RVM 是在系统范围内安装的,而不是附加到特定用户(/usr/local/rvm 中的文件) 这样 rvm 组中的所有用户都可以使用相同的已安装 gem 访问相同的 ruby​​.

Basically, this means there is no pre-build system ruby (all ruby installs are RVM-managed) and RVM is installed system-wide instead of attached to a particular user (files at /usr/local/rvm) so all users in the rvm group can access the same rubies with the same installed gems.

以这种方式设置系统的一个问题是必须在 shell 会话中设置 rvm 环境,然后才能使用 ruby​​.对于所有 rvm 用户,我将其放在他们的 .bashrc 中:source "/usr/local/rvm/scripts/rvm".这适用于 ssh 会话.

One issue with setting up the system this way is that the rvm environment must be set up in a shell session before ruby can be used. For all rvm users, I put this in their .bashrc: source "/usr/local/rvm/scripts/rvm". This works fine for ssh sessions.

问题出现在不执行 .bashrc 的 cron 作业中.上面的 rvm 脚本 (/usr/local/rvm/scripts/rvm) 比设置几个环境变量要复杂得多,所以我实际上想在文件中的每个作业之前运行这个命令.

The problem comes in play for cron jobs, which don't execute .bashrc. The rvm script above (/usr/local/rvm/scripts/rvm) is considerably more complicated than setting up a few environment variables, so I'd actually like to run this command prior to every job in the file.

当然,我可以手动完成,就像这样:

Sure, I could do that manually, like so:

1 2 * * * source "/usr/local/rvm/scripts/rvm"; /do/some/cron/job/1
3 4 * * * source "/usr/local/rvm/scripts/rvm"; /do/some/cron/job/2
5 6 * * * source "/usr/local/rvm/scripts/rvm"; /do/some/cron/job/3
7 8 * * * source "/usr/local/rvm/scripts/rvm"; /do/some/cron/job/4

但我更愿意做这样的事情:

But I'd prefer to do something like this:

[execute] source "/usr/local/rvm/scripts/rvm"

1 2 * * * /do/some/cron/job/1
3 4 * * * /do/some/cron/job/2
5 6 * * * /do/some/cron/job/3
7 8 * * * /do/some/cron/job/4

显然,上述语法不起作用.但是,有什么方法可以让它发挥作用吗?cron 手册页和文档在这里没有太大帮助.但是否有一些技巧或标准方法可以实现这一目标?

Obviously, the above syntax doesn't work. But, is there some way to get this to work? The cron man pages and documentation were not of much help here. But is there some trick or standard way to achieve this?

如果有关系,我正在运行 Ubuntu 10.10 (Maverick Meerkat).

If it matters, I'm running Ubuntu 10.10 (Maverick Meerkat).

推荐答案

您不需要编写包装器(按照该逻辑,您不妨为包装器编写包装器).请保持简单.您需要做的就是配置您的 cron 作业以启动 bash shell,并使该 bash shell 加载您的环境.

You don't need to write wrappers (following that logic, you might as well write a wrapper to the wrapper). Please keep things simple. All you need to do is configure your cron job to launch a bash shell, and make that bash shell load your environment.

脚本中的 shebang 行不应直接引用 ruby​​ 可执行文件,而应引用 rvm 的 ruby​​:

The shebang line in your script should not refer directly to a ruby executable, but to rvm's ruby:

#!/usr/bin/env ruby

这会指示脚本加载环境并运行 ruby​​,就像我们在命令行上加载 rvm 一样.

This instructs the script to load the environment and run ruby as we would on the command line with rvm loaded.

在许多 UNIX 派生系统上,crontab 可以在定义要运行的作业的实际行之前有一个配置部分.如果是这种情况,您将指定:

On many UNIX derived systems, crontabs can have a configuration section before the actual lines that define the jobs to be run. If this is the case, you would then specify:

SHELL=/path/to/bash  

这将确保从 bash 生成 cron 作业.仍然缺少您的环境,因此要指示 bash 加载您的环境,您需要将以下内容添加到配置部分:

This will ensure that the cron job will be spawned from bash. Still, your environment is missing, so to instruct bash to load your environment, you will want to add to the configuration section the following:

BASH_ENV=/path/to/environment (typically .bash_profile or .bashrc) 

HOME 自动派生自 crontab 所有者的/etc/passwd 行,但您可以覆盖它.

HOME is automatically derived from the /etc/passwd line of the crontab owner, but you can override it.

HOME=/path/to/home

此后,cron 作业可能如下所示:

After this, a cron job might look like this:

15 14 1 * *     $HOME/rvm_script.rb

如果您的 crontab 不支持配置部分怎么办?好吧,您必须将所有环境指令与作业本身一起放在一行中.例如,

What if your crontab doesn't support the configuration section? Well, you will have to give all the environment directives in one line, with the job itself. For example,

15 14 1 * * export BASH_ENV=/path/to/environment && /full/path/to/bash -c '/full/path/to/rvm_script.rb'

关于该主题的完整博文

这篇关于需要在每个 cron 作业之前设置 rvm 环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆