[Ruby] Restart Unicorn

1 minute read

Premise

I have a Rails app deployed on EC2 and using Unicorn as an application server.

If it was a development environment, I should have started it with rails s, but since the method of starting and stopping the server has changed in the production environment, I have summarized it as a memo.

If you want to stop the server

Unicorn startup confirmation

ps -ef | grep unicorn | grep -v grep
vinaka 15533 1 0 08:02 ?00:00:01 unicorn_rails master -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production
vinaka 15537 15533 0 08:03? 00:00:00 unicorn_rails worker[0] -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production
vinaka 15538 15533 0 08:03? 00:00:00 unicorn_rails worker[1] -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production

``master, worker[0], worker[1]’’ If you see three, Unicorn seems to be running.

kill

Kill'' the top number (master)15533 `` of the three I just confirmed ((The number changes every time so **ps -ef grep unicorn grep -v grep* (Check the number with *)
kill -9 15533

Unicorn stop confirmation

ps -ef | grep unicorn | grep -v grep


Then nothing should be displayed. If not, Unicorn is stopped.

Procedure for restarting the server

Unicorn stop confirmation

ps -ef | grep unicorn | grep -v grep


Launch Unicorn!

 bundle exec unicorn_rails -c /var/www/rails/appname/config/unicorn.conf.rb -D -E production


If nothing comes out, it seems to be running properly.

I will check it for the time being.

ps -ef | grep unicorn | grep -v grep
vinaka 15740 1 1 08:48 ?00:00:01 unicorn_rails master -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production
vinaka 15744 15740 0 08:48? 00:00:00 unicorn_rails worker[0] -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production
vinaka 15745 15740 0 08:48? 00:00:00 unicorn_rails worker[1] -c /var/www/rails/ShitsumonWa-/config/unicorn.conf.rb -D -E production

Three are displayed! It has been started. The number should have changed just before!

By the way

master failed to start, check stderr log for details

If you get an error

cat log/unicorn.log

Check the error content with. Unicorn version (It seems that an error will occur if it is 5.5 or more, so it is better to specify the version in Gem file)

Gemfile


group :production, :staging do
    gem'unicorn', '5.4.1'
end

Also

unicorn_rails -c /var/www/rails/appname/config/unicorn.conf.rb -D -E production

It may get angry, so it is better to add bundle exec.

bundle exec unicorn_rails -c /var/www/rails/appname/config/unicorn.conf.rb -D -E production

nginx also restarts

When Unicorn is launched successfully, restart nginx and you’re done.

sudo nginx -s reload

It is the end.