【问题标题】:Ubuntu: 'unicorn_init.sh start' works, but 'service unicorn_init start' does NOTUbuntu:“unicorn_init.sh start”有效,但“service unicorn_init start”无效
【发布时间】:2012-05-14 07:41:37
【问题描述】:

我在 Ubuntu 12.04 服务器上安装了带有 unicorn 的 nginx。一切正常,站点,数据库,独角兽......很好。所以我试图确保在重新启动后,nginx 和独角兽启动。我为我的独角兽进程设置了 update-rc.d,但它在重新启动后无法启动/工作。我怀疑这与 ubuntu 使用“服务”而不是“/etc/init.d/unicorn_init”有关

换句话说:

如果我执行:

$ /etc/init.d/unicorn_init start

unicorn 启动正常,没有错误。

如果我执行:

$ service unicorn_init start

它失败了,独角兽没有启动。

我认为它与路径有关。我已将环境 PATHS 添加到 PATH、GEM_PATH 和 GEM_HOME,但我仍然收到相同的结果

1,如果我运行 /usr/local/rvm/gems/ruby-1.9.3-p194/bin/unicorn,我会得到错误:

usr/local/rvm/rubies/ruby-1.9.3-p194/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 /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:256:in `to_spec'
    from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems.rb:1231:in `gem'
    from /usr/local/rvm/gems/ruby-1.9.3-p194/bin/unicorn:18:in `<main>'

2,如果我运行 /var/rails/web-app/bin/unicorn,我会得到错误:

/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- bundler/setup (LoadError)
    from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /var/rails/web-app/bin/unicorn:14:in `<main>'

任何帮助将不胜感激!谢谢

【问题讨论】:

    标签: ruby-on-rails ubuntu nginx rvm unicorn


    【解决方案1】:

    您应该使用包含所有必需环境变量的 unicorn 包装脚本:

    rvm wrapper 1.9.3 ruby-1.9.3 unicorn
    

    它将生成ruby-1.9.3_unicorn 在初始化脚本中使用它而不是独角兽。

    您可以通过以下方式找到有关包装器的更多详细信息:

    rvm wrapper
    

    如果通过捆绑程序(如 capitrano)完成工作,则为 bundle 生成一个包装器:

    rvm wrapper 1.9.3 ruby-1.9.3 bundle
    

    并使用此命令显示的包装器的完整路径:

    which ruby-1.9.3_bundle
    

    【讨论】:

    • 是的!谢谢你。我这样做了,将加载路径更改为 /usr/local/rvm/bin/ruby-1.9.3_unicorn 并且 $ service unicorn_init start 成功运行。尝试重启,独角兽成功启动。非常感谢!
    【解决方案2】:

    你说的对,Eric,我自己做,在开发模式下运行就可以了。 这个例子不能正常使用,还是很粗糙的。

    我的config/unicorn_init 文件:

    TIMEOUT=${TIMEOUT-60}
    PID=$APP_ROOT/tmp/pids/unicorn.pid
    CMD="PATH=$_PATH GEM_HOME=$_GEM_HOME GEM_PATH=$_GEM_PATH $APP_ROOT/.bundle/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb"
    
    set -e
    action="$1"
    set -u
    
    old_pid="$PID.oldbin"
    
    cd $APP_ROOT || exit 1
    
    sig () {
        test -s "$PID" && kill -$1 `cat $PID`
    }
    
    oldsig () {
        test -s $old_pid && kill -$1 `cat $old_pid`
    }
    
    case $action in
    start)
        sig 0 && echo >&2 "Already running" && exit 0
        su -c "$CMD" - $APP_USER
        ;;
    stop)
        sig QUIT && exit 0
        echo >&2 "Not running"
        ;;
    force-stop)
        sig TERM && exit 0
        echo >&2 "Not running"
        ;;
    restart|reload)
        sig HUP && echo reloaded OK && exit 0
        echo >&2 "Couldn't reload, starting '$CMD' instead"
        su -c "$CMD" - $APP_USER
        ;;
    upgrade)
        if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
        then
            n=$TIMEOUT
            while test -s $old_pid && test $n -ge 0
            do
                printf '.' && sleep 1 && n=$(( $n - 1 ))
            done
            echo
    
            if test $n -lt 0 && test -s $old_pid
            then
                echo >&2 "$old_pid still exists after $TIMEOUT seconds"
                exit 1
            fi
            exit 0
        fi
        echo >&2 "Couldn't upgrade, starting '$CMD' instead"
        su -c "$CMD" - $APP_USER
        ;;
    reopen-logs)
        sig USR1
        ;;
    *)
        echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
        exit 1
        ;;
    esac
    
    echo "#\!/bin/bash\n_PATH=$PATH\n_GEM_HOME=$GEM_HOME\n_GEM_PATH=$GEM_PATH\nAPP_ROOT=$(pwd)\nAPP_USER=$USER\n$(cat config/unicorn_init)" > config/unicorn_init.sh
    
    chmod +x config/unicorn_init.sh
    

    【讨论】:

      【解决方案3】:

      这是 6 年前对这个问题/答案的更新。从 RVM 1.29.4Ubuntu 16.04 开始。来自 mpapis 的答案对于旧版本的 ubuntu 和 rvm 仍然有效。

      RVM 1.29.4 开始,上述答案 vm wrapper 1.9.3 ruby-1.9.3 unicorn 不再有效,也不再需要。

      包装器已经创建,如果需要,可以在它们的位置以及适当的 gemset 中找到。

      查看以下目录位置/usr/local/rvm/wrappers。在那里你会找到你想要的 ruby​​ 版本和 gemset 的链接。点击该链接将带您访问它的所有包装器:unicorn, unicorn_rails, god, puma, thin, thor, ...等。

      例子:

      TIMEOUT=${TIMEOUT-60}
      
      APP_ROOT=/var/rails/com.domain.site/current
      PID=$APP_ROOT/tmp/pids/unicorn.pid
      DAEMON=/usr/local/rvm/wrappers/ruby-2.5.1@app/unicorn
      
      CMD="$DAEMON -D -c $APP_ROOT/config/unicorn.rb -E production"
      

      或者,您也可以使用直接路径:

      DAEMON=/usr/local/rvm/gems/ruby-2.5.1@app/wrappers/unicorn
      

      你懂的(^_^)

      【讨论】:

        猜你喜欢
        • 2017-08-27
        • 1970-01-01
        • 1970-01-01
        • 2019-12-07
        • 1970-01-01
        • 1970-01-01
        • 2021-03-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多