Automatically start Rails applications after reboot

Create a simple shell script that restarts your Ruby on Rails application's unicorn instance.

Thomas V.

1 minute read

Convert the deploy:start functionality of your existing capistrano deploy recipe to a shell script:

# /var/www/app/
cd /var/www/app/current &&
(PATH=$HOME/.rbenv/shims:$HOME/.rbenv/bin: RAILS_ENV=production
/usr/bin/env bundle exec unicorn -c /var/www/app/current/config/unicorn.rb -E production -D)

Make it executable:

chmod u+x /var/www/app/

Add it to /etc/rc.local (be sure to replace app with whatever your applications user is):

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

su -c '/home/app/' app &
exit 0

After this you should be able to reboot your box and unicorn will boot up automatically allowing NGINX to resume serving pages.

comments powered by Disqus