【问题标题】:Require in Init.d script在 Init.d 脚本中需要
【发布时间】:2016-10-16 21:03:30
【问题描述】:

我已经编写了一个 init.d 脚本来启动一个 newrelic 插件作为守护进程。问题是当我运行service rb_nr_agent start 时,它有一些与“require”相关的错误。输出:

[root@device newrelic_rb_plugin]# /usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- snmp (LoadError)
from /usr/local/rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /opt/newrelic_rb_plugin/newrelic_redborder_agent:5:in `<main>'  

[root@device newrelic_rb_plugin]# ./rb_nr_agent start Starting rb_nr_agent:[ OK ]

它无法正常启动。当我运行相同的脚本但在项目的根路径中它没有任何错误并且工作正常。 init.d 是那个的副本。这里有脚本的启动选项:

start() {
    RESULT=`ps aux | grep $executable | grep -c -v grep`
    if [ "${RESULT:-null}" -ge "1" ]; then
      echo "$prog is currently running"
    else
      echo -n "Starting $prog: "
      /opt/newrelic_rb_plugin/newrelic_redborder_agent > /dev/null &
      RETVAL=$?
      if [ $RETVAL -eq 0 ]; then
       echo_success
      else
       echo_failure; failure
       RETVAL=1
      fi
      echo
    fi 
    return $RETVAL }

【问题讨论】:

  • 在目标文件的顶部 (/opt/newrelic_rb_plugin/newrelic_redborder_agent) 添加puts $LOAD_PATH 并使用service ... start 运行它。之后以它的工作方式再次运行它并检查是否有任何差异。

标签: ruby init.d


【解决方案1】:

错误文本表明您正在使用 RVM,但它仅在用户登录时加载,因此默认情况下在 init-scripts 中不可用。

使用 rvm do 在启用 rvm 的情况下运行命令:

/usr/local/rvm/bin/rvm ruby-2.1.2 do /opt/newrelic_rb_plugin/newrelic_redborder_agent > /dev/null &

(您可能需要更正确切安装的 ruby​​ 版本和 gemset 名称,如果使用的话)

【讨论】:

  • 它可以工作,但我需要在脚本中运行cd PATH_TO_PROJECTpreviously,因为配置目录在 NR Ruby SDK 中不可配置。非常感谢。
猜你喜欢
  • 2010-12-31
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 2013-09-29
  • 2011-04-05
  • 1970-01-01
  • 2013-07-23
  • 1970-01-01
相关资源
最近更新 更多