Josué Lima
Josué Lima

Reputation: 483

Why do I get an error when starting unicorn when the system boots?

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:

Upvotes: 3

Views: 2829

Answers (3)

mpapis
mpapis

Reputation: 53178

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/

Upvotes: 3

Tilo
Tilo

Reputation: 33742

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.

Upvotes: 1

WarHog
WarHog

Reputation: 8710

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

Upvotes: -1

Related Questions