【问题标题】:Why do I get an error when starting unicorn when the system boots?为什么系统启动时启动独角兽会报错?
【发布时间】:2011-11-01 21:54:11
【问题描述】:

这是我的独角兽初始化脚本(/etc/init.d/unicorn):

#! /bin/sh

PATH=/home/josue/.rvm/gems/ruby-1.9.3-p0/bin:/home/josue/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/josue/.rvm/rubies/ruby-1.9.3-p0/bin:/home/josue/.rvm/bin:/usr/local/sbin:$
DAEMON=/home/josue/.rvm/gems/ruby-1.9.3-p0/bin/unicorn_rails

DAEMON_OPTS="-c /home/josue/sped/current/unicorn.rb -E production -D"
NAME=unicorn_rails
DESC=unicorn_rails
PID=/home/josue/sped/shared/pids/unicorn.pid

case "$1" in
  start)
        echo -n "Starting $DESC: "
        exec $DAEMON $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        kill -QUIT `cat $PID`
        echo "$NAME."
        ;;
  restart)
        echo -n "Restarting $DESC: "
        kill -QUIT `cat $PID`
        sleep 1
        $DAEMON $DAEMON_OPTS
        echo "$NAME."
        ;;
  reload)
        echo -n "Reloading $DESC configuration: "
        kill -HUP `cat $PID`
        echo "$NAME."
        ;;
  *)
        echo "Usage: $NAME {start|stop|restart|reload}" >&2
        exit 1
        ;;
  esac

exit 0

当我以普通用户身份运行/etc/init.d/unicorn start 时,它工作正常,但是当我尝试以root 身份运行时,结果如下:

Starting unicorn_rails: /home/josue/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:247:in `to_specs': Could not find unicorn (>= 0) amongst [bigdecimal-1.1.0, io-console-0.3, json-1.5.4, minitest-2.5.1, rake-0.9.2.2, rdoc-3.9.4] (Gem::LoadError)
    from /home/josue/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
    from /home/josue/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/site_ruby/1.9.1/rubygems.rb:1210:in `gem'
    from /home/josue/.rvm/gems/ruby-1.9.3-p0/bin/unicorn_rails:18:in `<main>'

所以,当服务器启动时,unicorn 不会自动加载。

我正在使用:

  • ubuntu 10.04
  • rvm
  • 红宝石 1.9.3-p0

【问题讨论】:

    标签: ruby-on-rails ruby rvm unicorn


    【解决方案1】:

    有几种方法可以让它工作:

    1. 按照您的代码:

      PATH=/home/josue/.rvm/gems/ruby-1.9.3-p0/bin:/home/josue/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/josue/.rvm/rubies/ruby-1.9.3-p0/bin:/home/josue/.rvm/bin:$PATH
      GEM_HOME=/home/josue/.rvm/gems/ruby-1.9.3-p0
      GEM_PATH=/home/josue/.rvm/gems/ruby-1.9.3-p0:/home/josue/.rvm/gems/ruby-1.9.3-p0@global
      
    2. 使用 rvm 包装器:https://rvm.io/integration/init-d/

    3. 或其他人:https://rvm.io/integration/cron/

    【讨论】:

      【解决方案2】:

      如果您在生产环境中,您可能不想以 root 身份安装一些 gem,而其他一些 gem 会与 Rails 应用程序捆绑/安装...

      有一个简单的方法可以解决 OPs 问题:同时设置 GEM_PATH 和 GEM_HOME

      如果你正确设置了 root 帐户 (~/.bashrc) 的 PATH、GEM_PATH 和 GEM_HOME 环境变量,那么你将能够使其工作。 例如unicorn 可执行文件应位于 root 的 PATH 中,并且 GEM 相关的环境变量应正确设置为“捆绑安装”期间 gem 的安装位置(例如,这可以位于另一个用户的主目录中)。

      $ cat /root/.bashrc
      export PATH=/home/josue/.rvm/gems/ruby-1.9.3-p0/bin:/home/josue/.rvm/gems/ruby-1.9.3-p0@global/bin:/home/josue/.rvm/rubies/ruby-1.9.3-p0/bin:/home/josue/.rvm/bin:$PATH
      export GEM_HOME=/home/josue/.rvm/gems/ruby-1.9.3-p0/gems
      export GEM_PATH=/home/josue/.rvm/gems/ruby-1.9.3-p0/gems:/home/josue/.rvm/gems/ruby-1.9.3-p0@global/gems
      

      启动后,您还应该触摸一个文件 /var/lock/subsys/$APP_NAME 并在杀死 Unicorns 后删除该文件,以便您的 LINUX 系统知道您的应用程序正在运行。

      这对我在生产中非常有效。

      我通常将 /etc/init.d/unicorn 脚本重命名为我的应用程序的名称,以防我有多个应用程序正在运行。

      【讨论】:

        【解决方案3】:

        似乎 Unicorn gem 没有安装在 root 用户下。您是否尝试过以 root 身份登录然后安装它?

        【讨论】:

        • 这个脚本的缺点是您总是需要以 root 身份安装 Unicorn -- 与 Rails 应用程序的“捆绑安装”分开 -- 这意味着几个月后,更新 Rails 应用程序后,您最终可能仍然使用旧版/陈旧版本的 Unicorn。因此,我的回答重新更改 PATH、GEM_PATH、GEM_HOME 可能是在生产中执行此操作的更好方法。
        猜你喜欢
        • 2012-06-01
        • 2023-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-21
        • 2023-03-09
        • 2013-09-29
        相关资源
        最近更新 更多