unicorn upstart job

Asked by Federico Ramallo


I build an upstart job to run and unicorn server. But I have some issues tracking the correct pid.
I'm using upstart 0.6.5-8 on ubuntu 10.04

This is the job:
description "Gentlemans foro production site"
author "Federico Ramallo"

start on startup
stop on shutdown

chdir /home/gentlemans-foro
exec sudo -u gentlemans-foro -H -i -- 'cd ~/gentlemansforo ; export RAILS_ENV=production ; source .rvmrc; bundle exec unicorn_rails -p 3040 -c config/unicorn.rb -E production'

This is the current ps state when running the job above:

# initctl status gentlemans-foro.com.ar
gentlemans-foro.com.ar start/running, process 19536
# ps -Af | grep -P 'gentlemans|unicorn'
1006 6623 6508 0 13:15 pts/4 00:00:00 su gentlemans-foro
1006 19536 1 0 19:58 ? 00:00:00 -bash -c cd ~/gentlemansforo ; export RAILS_ENV=production ; source .rvmrc; bundle exec unicorn_rails -p 3040 -c config/unicorn.rb -E production
1006 19613 19536 3 19:58 ? 00:00:01 unicorn_rails master -p 3040 -c config/unicorn.rb -E production
1006 20161 19613 94 19:58 ? 00:00:04 unicorn_rails worker[0] -p 3040 -c config/unicorn.rb -E production

If I run unicorn on a bash script then upstart tracks the bash script pid.
If I run unicorn as daemon (same as above but using -D switch on unicorn command) upstart tracks the incorrect pid.
I tried both expect fork and expect daemon, but none work.

The script above works. And I track unicorn working with monit using the correct pid.
But I would like to set unicorn as daemon (-D) and reduce the amount of processes running.
Is it possible?

Also I would like to send a HUP signal to unicorn master process on a restart event. The reason for that is because unicorn can make a hot deployment when receive a HUP signal. If i kill the process and start it again then users could get an error during restart.

I tried a stanza to run the process as another user, but was not working at that time. Should I still use exec sudo?

Also I found that chdir was not working correctly, hence cd is my first command. Is this working on newer versions?

Thanks for your help!

Question information

English Edit question
upstart Edit question
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Giovanni (aka Vanni) Totaro (vanni-totaro) said :

As suggested here (http://upstart.ubuntu.com/cookbook/#alternative-method) you can use start-stop-daemon to solve this issue.

Your upstart config file exec line should be something like this:

exec start-stop-daemon --start --exec su -- -c 'cd /home/gentlemans-foro/gentlemansforo; export RAILS_ENV=production; source .rvmrc; bundle exec unicorn_rails -p 3040 -c config/unicorn.rb -E production' gentlemans-foro

No need for expect fork or expect daemon.

Can you help with this problem?

Provide an answer of your own, or ask Federico Ramallo for more information if necessary.

To post a message you must log in.