26 December, 2011

running a simple rails app (like wagn) with ubuntu upstart






I had a hell of a time getting my upstart script to start. It kept launching, reporting that it started and even showing a pid, but it would die. This was a version of the same script I had written in a shell script and it would execute just fine. The addition of the export HOME, suddenly made things come together.

I'd been chasing the various values in the expect stanza, but nothing made sense. Once I had the HOME export in place, I was able to find the right setting for the expect stanza after a couple of tries.

Anyway, I wanted to run a very simple rails app, called wagn, as an internal tool on an internal server. In the event that our box goes down, I want wagn to restart. Furthermore, I don't have a bunch of time to muck about with all of those full-on production ways of deploying ruby, so I went looking for the best way to run this on the platform I had in front of me. I found out the new way of running services on ubuntu is through upstart.

After looking through a bunch of things on upstart:

I found a hint... well, something I hadn't tried yet. Anyway, upstart seems to need a HOME var set when you execute. So, if you've been searching, here's a good example to go on if you want to run a rails server with upstart. You can create something similar to the following in /etc/init/servicename.conf. If you are using it specifically for wagn, edit my path below to match your install location for wagn.

# example script offered with NO WARRANTY. 
# feel free to use it or just learn from it

# wagn-server - wagn-server job file
description "wagn server"
author "robert tomb"

# restart process if crashed try 10 times every 5
seconds
respawn
respawn limit 10 5
# let upstart know the process will detach
expect daemon

# When to start the service
start on runlevel [234]

# When to stop the service
stop on runlevel [016]

# don't know why but home=root is necessary
# found it here: http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/
script
   export HOME="/root"
   exec /usr/local/wiki/wagn-1.7.0/script/server -b 192.168.1.54 -e production -p 3000 -d > /var/log/wagn.log 2>&1
end script

No comments: