-
run-it
I deployed a NodeJS Express app behind an nginx reverse proxy and wanted to have the node process supervised so it would automatically be restarted after crashes. I learned about a nifty service supervision suite called runit (inspired by D.J. Bernstein’s daemontools) from working at Cloudkick that would be perfect for the job.
runit has a prgram called runsvdir that monitors a particular directory for subdirectories that represent services. It launches a runsv process for every subdirectory and runsv executes a run script that you create to execute your service. In the Debian runit package, runsvdir watches /etc/service though you should create your service directory under /etc/sv and symlink it into /etc/service so you can test your run scripts.
The catch was, I wanted to run node with an unprivileged user and have the service directory in my home directory so I didn’t have to bother with sudoing. This is possible by having another runsvdir process monitor /home/mark/service. Here’s how I set it up:
/etc/sv/ |-- runsvdir-mark/ |-- log/ | |-- main/ | `-- run* `-- run* /etc/service/ `-- runsvdir-mark@ -> /etc/sv/runsvdir-mark/ /etc/sv/runsvdir-mark/run: #!/bin/sh exec 2>&1 exec chpst -u mark runsvdir /home/mark/service/ /etc/sv/runsvdir-mark/log/run: #!/bin/sh exec chpst svlogd -tt ./main/ .With that, there’s now a runsvdir process monitoring /home/mark/service. I then created:
/home/mark/service |-- aubergine-0/ |-- log/ | |-- main/ | `-- run* `-- run* /home/mark/service/aubergine-0/run: #!/bin/sh exec 2>&1 exec chpst -e /home/mark/service/aubergine-0/env \ node /home/mark/code/aubergine/aubergine.js /home/mark/service/aubergine-0/log/run: #!/bin/sh exec chpst svlogd -tt ./main/