- Use chkconfig — about, link
- DaemonController library. It will enable you to auto-start services with your Rails app, starting them if they aren’t already started.
- Create an initializer containing (about):
Thread.new do
system("rackup faye.ru -s thin -E production")
end
-
On Ubuntu, you should use the operating systems’s init system — Upstart.
user@host:~$ cat /etc/init/faye.conf description "Faye Upstart script" start on startup stop on shutdown respawn script env RAILS_ENV=production exec sudo -u deployuser -i /home/deployuser/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/bin/rackup /var/www/booko.com.au/booko/faye.ru -s thin -E production end script
I’m not happy with the method of calling Ruby since it will change. But the advantages are that it will start when the system starts and it will respawn if it dies or you KILL it.
Let Upstart take care of demonising a process and making sure it keeps running
-
Shell script in config/thin_example.sh
#!/bin/sh set -e # Feel free to change any of the following variables for your app: TIMEOUT=${TIMEOUT-60} APP_ROOT=/home/deployer/apps/example/current PID=$APP_ROOT/tmp/pids/thin.pid CMD="cd $APP_ROOT; bundle exec rackup -D -P $PID $APP_ROOT/config/faye.ru -s thin -E production" AS_USER=deployer set -u startme(){ run "$CMD"} stopme(){ run "pkill -f $PID"} run (){if["$(id -un)"="$AS_USER"];then eval $1 else su -c "$1"- $AS_USER fi }case"$1"in start) startme ;; stop) stopme ;; restart) stopme; startme ;;*) echo "usage: $0 start|stop|restart">&2 exit 1;; esac
Loosely modified from the unicorn scripts that Ryan Bates used in his VPS deployment railscast (pro only).
Make it executable
chmod +x config/thin_example.sh
You’ll need to symlink it to init.d (after chmod +x ‘ing to make it executable)
sudo ln -nfs /home/deployer/apps/example/current/config/thin_example.sh /etc/init.d/thin_example
Then if you want it to startup with the server
sudo update-rc.d thin_example defaults
Otherwise you should just be able to
/etc/init.d/thin_example [start|stop|restart]
. An important point to note is that I’m telling rackup to start in daemon mode (-D) and explicitly setting the PID so I can kill it late