为什么bash -l -c"CMD"使红宝石找到我的宝石? [英] why bash -l -c "CMD" makes ruby find my gem?

查看:118
本文介绍了为什么bash -l -c"CMD"使红宝石找到我的宝石?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ruby脚本中,我需要gmail宝石:

In my ruby script,I required the gmail gem:

require 'rubygems'
require 'gmail'

require 'rubygems'
require 'gmail'

在shell中运行时,它可以正常运行:

when running in shell,it works ok:

ruby my-script.rb

当我将其放入cron作业时,它无法执行:

while when I put it in a cron job,it failed to execuate:

* * * * * cd /to/script/directory;/usr/local/rvm/rubies/ree-1.8.7-2011.03/bin/ruby ./my-script.rb

日志显示无法加载gmail gem:

the log shows that the gmail gem can not be loaded:

/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- gmail (LoadError)

/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- gmail (LoadError)

好吧,当我这样做时(将cmd放入bash -l -c'CMD'):

well, when i do this(put the cmd in bash -l -c 'CMD'):

* * * * * /bin/bash -l -c 'cd /to/script/directory;/usr/local/rvm/rubies/ree-1.8.7-2011.03/bin/ruby ./my-script.rb'

可以再次正常工作.

为什么?

ps.我知道arg -l使bash成为登录shell,但这有什么区别吗?

ps.I know the arg -l make the bash a login shell,but does that make any difference?

推荐答案

-l参数在登录Shell中执行命令,这意味着它从Shell配置文件继承了您的路径和其他设置.如果cron作业在没有登录外壳的情况下运行,则将在未设置任何路径环境变量(例如RVM设置的路径环境变量)的情况下运行,这将导致系统无法找到所引用的gem.

The -l parameter executes the command in a login shell, which means that it inherits your path and other settings from your shell profile. The cron job, if run without the login shell, will be run without any path environment variables set (such as those set by RVM), which results in the system being unable to find the referenced gems.

这篇关于为什么bash -l -c"CMD"使红宝石找到我的宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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