Today I’ve been updating the TrustAuth project website to reflect the changes I made the last couple of months. While I was deploying, I rediscovered the terrible state of the Ubuntu init script I had been using to run Unicorn. It wouldn’t properly stop Unicorn so I had to SSH into my server and restart Unicorn when I updated the website. If I didn’t it would still be serving the old Rails pages. So tonight I set out to fix this.
I started by trying to figure out why the script wasn’t creating the file for the PID. Looking through the script I had found in this setup guide I realized that it wasn’t writing the PID anywhere. I tried to use pidof to output the PID of the process but it wasn’t finding any unicorn_rails processes despite there being five listed with ps aux. This gave me the idea to look at one of the existing init scripts that I have been using already; specifically the Nginx script.
In the script I found that every command was using start-stop-daemon so I decided to look if it would work with Unicorn. I wasn’t sure because unicorn_rails is actually a Ruby script that launches Unicorn. Sure enough with a little bit of tinkering I was able to get a working init script that starts, stops, and restarts Unicorn. Not only that but start-stop-daemon outputs the PID so I have that too. Here’s the init script based off the one from the previously mentioned guide and with my modifications:</p>
which unicorn_rails I don’t need to update the script when I update the version of Ruby I’m using. I hope this helps you as much as it has helped me. I tested this on Ubuntu Lucid Lynx with Ruby 1.9.3-p194.