Andrew
Andrew

Reputation: 7883

How do I restart my Unicorn server?

I have a server which is running Unicorn and nginx. I've just done a git pull to get the most recent updates to my own code, and now I need to restart it.

In setting it up, I followed this guide, and did these steps:

Start Unicorn

cd /var/www/my_server

unicorn -c unicorn.rb -D

Restart nginx

service nginx restart

I now need to know how to restart it. Ideally, it should be a quick process, so that my server doesn't have any/much downtime when doing this in the future.

EDIT: I tried a few other things as suggested elsewhere, such as killall ruby, and rebooting my server. Now I'm at a point where I've done the above, it doesn't give me any errors, but when I try and load a page, it doesn't respond, and likely eventually times out (though I didn't leave it that long). If I stop nginx, it says "connection refused", so it's obvious that nginx is working, but for some reason it's not able to connect to Unicorn.

EDIT: On a whim, I typed in just unicorn and it seems to be having an issue with my project - missing gems. Makes sense. So that first edit is no longer an issue, I'm still interested in the most elegant way of restarting it.

Upvotes: 1

Views: 3643

Answers (3)

Sir l33tname
Sir l33tname

Reputation: 4330

I would recommend to use the init script they provide

One example init script is distributed with unicorn:
http://unicorn.bogomips.org/examples/init.sh

because there you have the upgrade method which takes care of your use case, restarting the app with zero downtime.

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

Upvotes: 1

Horacio
Horacio

Reputation: 2965

You can try send a HUP signal to the master process, doing

 kill -HUP <processID>

HUP - reloads config file and gracefully restart all workers. If the "preload_app" directive is false (the default), then workers will also pick up any application code changes when restarted. If "preload_app" is true, then application code changes will have no effect; USR2 + QUIT (see below) must be used to load newer code in this case. When reloading the application, +Gem.refresh+ will be called so updated code for your application can pick up newly installed RubyGems. It is not recommended that you uninstall libraries your application depends on while Unicorn is running, as respawned workers may enter a spawn loop when they fail to load an uninstalled dependency.

If you want to read more about unicorn's signals you can read more here

Upvotes: 2

Tony Vincent
Tony Vincent

Reputation: 14282

On Ubuntu 14.04 try

sudo service unicorn restart 

If you are using capistrano add this in deploy.rb

desc "Zero-downtime restart of Unicorn"
task :restart, :except => { :no_release => true } do
  run "kill -s USR2 unicorn_pid"
end

Upvotes: 1

Related Questions