为什么我在系统启动时启动 unicorn 时出现错误?

发布于 2024-12-13 07:43:09 字数 1905 浏览 0 评论 0原文

这是我的独角兽初始化脚本(/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

This is my init script for 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

When I run /etc/init.d/unicorn start logged in as normal user, it works fine, but when I try to run as root, this is the result:

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>'

So, when the server starts, unicorn is not loaded automatically.

I'm using:

  • ubuntu 10.04
  • rvm
  • ruby 1.9.3-p0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江南烟雨〆相思醉 2024-12-20 07:43:09

有几种方法可以使其工作:

  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/

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/
吃颗糖壮壮胆 2024-12-20 07:43:09

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

有一种简单的方法可以修复OP问题:还要设置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 脚本重命名为我的应用程序的名称,以防我运行多个应用程序。

If you are in a Production environment, you probably don't want to install some of your gems as root, and some other gems get bundled/installed together with the Rails application...

There is an easy way to fix the OPs problem: also set GEM_PATH and GEM_HOME

If you correctly set the PATH, GEM_PATH and GEM_HOME environment variables for the root account (~/.bashrc) , then you will be able to make it work.
e.g. the unicorn executable should be in root's PATH, and the GEM-related env-variables should be set correctly to where the gems are installed during "bundle install" (e.g. this can be in another user's home directory).

$ 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

After start, you should also touch a file /var/lock/subsys/$APP_NAME
and remove that file after killing the Unicorns , so that your LINUX system knows that your application is running.

This works very well for me in production.

I typically rename the /etc/init.d/unicorn script to the name of my application, in case I have several Apps running.

神回复 2024-12-20 07:43:09

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

It seems the Unicorn gem is not installed under root user. Have you tried to login as root and then install it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文