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

查看:118
本文介绍了需要在每个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 组中的所有用户都可以使用相同的已安装gems访问相同的rubies。

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.

以这种方式设置系统的一个问题是,在使用ruby之前,必须在shell会话中设置rvm环境。对于所有rvm用户,我把它放在他们的 .bashrc source/ usr / local / rvm / scripts / rvm。这对于s​​sh会话很好用。

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.

问题出在cron作业,它不执行 .bashrc 。上面的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

但我更喜欢这样做:

[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

在许多UNIX派生系统上,crontabs在实际线路之前可以有一个配置部分定义要运行的作业。如果是这样,您将指定:

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  

这将确保cron作业将从bash中生成。仍然,您的环境缺失,所以要指示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) 

派生自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天全站免登陆