简体   繁体   中英

Ubuntu: 'unicorn_init.sh start' works, but 'service unicorn_init start' does NOT

I have installed nginx with unicorn on an Ubuntu 12.04 server. Everything works, site, db, unicorn...good. So I'm trying to make sure that after a reboot, nginx and unicorn start up. I have set update-rc.d for my unicorn process, but it doesn't start/work after a reboot. I suspect it has something to do with ubuntu's use of "service" as opposed to "/etc/init.d/unicorn_init "

In other words:

If I execute:

$ /etc/init.d/unicorn_init start

unicorn starts up just fine, no errors.

If I execute:

$ service unicorn_init start

it fails and unicorn does not start.

I've think it has something to do with paths. Ive added environment PATHS to PATH, GEM_PATH, & GEM_HOME, but I still receive the same results

1, If I run /usr/local/rvm/gems/ruby-1.9.3-p194/bin/unicorn, I get errors:

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, If I run /var/rails/web-app/bin/unicorn, I get errors:

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

Any help would be greatly appreciated! Thanks

you should use an unicorn wrapper script that will include all the required environment variables:

rvm wrapper 1.9.3 ruby-1.9.3 unicorn

It will generate ruby-1.9.3_unicorn use this instead of just unicorn in the init script.

You can find more details on wrappers with:

rvm wrapper

In case when work is done via bundler (like capitrano) then generate a wrapper for bundle :

rvm wrapper 1.9.3 ruby-1.9.3 bundle

and use the full path to the wrapper as displayed by this command:

which ruby-1.9.3_bundle

You told right, Eric, I do it by myself and run in development mode is fine. This example can't be used properly, it is still very crude.

My config/unicorn_init file:

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

This is an update to this question/answer from 6 years ago. As of RVM 1.29.4 and Ubuntu 16.04 . The answer from mpapis is still valid for older versions of ubuntu and rvm.

As of RVM 1.29.4 , the above answer vm wrapper 1.9.3 ruby-1.9.3 unicorn no longer works, nor is it neccessary anymore.

Wrappers are already created, and can be found in their location along with the proper gemset if needed.

Look in the following directory location /usr/local/rvm/wrappers . There you will find a link to your desired ruby version & gemset. Following that link will bring you to all of it's wrappers: unicorn, unicorn_rails, god, puma, thin, thor, ... , etc.

Example:

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"

Alternatively you could also use the direct path as well:

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

You get the idea (^_^)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM