当系统启动时,为什么我启动unicorn会出现错误?

3
这是我的unicorn初始脚本(/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
  • ruby 1.9.3-p0
3个回答

3

There are a few ways to make it work:

  1. Following your code:

    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. Using rvm wrappers: https://rvm.io/integration/init-d/

  3. Or others: https://rvm.io/integration/cron/

1

如果您在生产环境中,可能不希望将某些 gem 作为 root 安装,而其他一些 gem 则与 Rails 应用程序捆绑/安装在一起...

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

如果您正确地为 root 帐户(~/.bashrc)设置了 PATH、GEM_PATH 和 GEM_HOME 环境变量,则您将能够使其正常工作。例如,unicorn 可执行文件应该在 root 的 PATH 中,并且与 gem 相关的 env 变量应该正确设置为在 "bundle install" 期间安装 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 脚本重命名为我的应用程序名称,以防我有多个应用程序在运行。


-1

看起来 Unicorn gem 没有在 root 用户下安装。您尝试过以 root 身份登录然后安装吗?


1
这个脚本的缺点是,你总是需要以 root 用户身份安装 Unicorn -- 与你的 Rails 应用程序的 "bundle install" 分开 -- 这意味着几个月后,在更新 Rails 应用程序后,你可能仍然在使用旧版本/陈旧的 Unicorn。因此,在生产环境中,我认为更好的方法是改变 PATH、GEM_PATH 和 GEM_HOME。 - Tilo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接