A Supervisor for Node.js

So, I’m writing this blog post because, despite how simple a concept the “Supervisor” is – I feel like I could have easily missed this super handy tool. In fact, I might go so far as to say that if I hadn’t had co-workers push the Python supervisor on me – I would have had a problem I just wouldn’t quite know how to solve.

Last February, I started learning Python because I had some downtime at work (front end dev wise) and I had an interest to get into the server side tool all of our devs were using. It was a fair bit to take in…Python was a new and fairly different language than what I was used to. It turned out fairly enjoyable.

Anyway – there was lots of stuff thrown at me, making CentOS VMs, administering Unix, all that stuff. You know, that stuff that doesn’t have much to do with programming – but the environment/platform itself. In the midst of running learning how we have our own package manager in company for locking down Python Eggs, the “Supervisor” was thrown at me out of the blue.

At first I thought it was some half cocked idea our developers put together in-house. What is this thing? Why do I need it? I just want to code some Python! I truly wanted to ignore it, because I never had a good idea what it was setting out to accomplish – plus it had some separate config files…just ick.

In the end though – I finally understood it. Once I got past the code, and got things into production – only then was the time it was really needed. See…a supervisor process will run alongside your code or server. If it goes down, due to an error you didn’t forsee, supervisor keeps tabs on it, and will just start it up again. Just log the error for viewing later – with the important thing being that your server or your process stays up.

Months later, I found myself with my first Node.js/Express webserver project that I wanted to make sure it stayed up. Yes – I could make DAMN sure to test, test, and test. Make sure no errors happened to bring down the server. Despite my resolution of no fatal errors (yeah, right) – I looked into the Python equivalent of supervisor for Node.js. Yep – I found one. It’s called Node-Supervisor.

Simply do a global NPM install and then start your project with it:

npm install supervisor -g
supervisor myapp.js

From that, we have a nice little process that monitors your Node.js app. If it goes down due to fatal errors, your script will get started right back up. The HUGE bonus to this is that I’m always iterating my project. My version number will keep going up as I add more to my project. That’s the other benefit here….If I do a git pull, then a checkout of my latest branch/tag, supervisor will restart my Node app with the latest version.  No hunting and killing the process, no restarting manually. Just pull and checkout, and the latest is instantly running.

All thanks to Supervisor.

Again – feels simple. Feels like everyone in the world already probably knows this. But me, not being a fully focused server side dev – well I might have easily missed this. A problem I might not have known I had, with a solution I wasn’t really looking for. But there it is…Supervisor is pretty sweet!

2 thoughts on “A Supervisor for Node.js”

  1. There’s such a long history of supervising middleware… supervisord (different older sw), runit, &c, and now supervisor is more middleware for doing that.

    I don’t often defend monolithic/big software, but the job of Process 1 (the init process) on Linux is to run software, and some Process 1’s also make it their business to keep software running, obviating the need for more runner middleware. Systemd and Upstart, Red Hat and Canonical’s recent init processes, both will restart software that fails: Systemd even has watchdog capabilities to restart software that’s running but not responding.

    I’ve used supervisor and I found it very handy, reliable and incredibly easy to use, but tooling up and learning more about what your OS says for keeping processes going is extra-credit that some people might appreciate, and- importantly- it’s knowledge that’ll port to whatever piece of software you’re interested in. The new PID1’s are pretty awesome robust pieces of software, so perhaps check em out?

    1. Good (and interesting) point. This level of detail might be lost on me and others because of the cross-platform expectations. I personally wouldn’t say I *have* an OS – I’m just writing software. I try not to do system specific things if I’m writing things where I don’t know what OS it will end up on. But of course – I could make sure to research how to keep these processes alive across Windows, Mac, and Linux (with all the variants if that factors in). A cross platform supervisor sure is easier though! Thanks, and again great points which I only really respond to with favoring time spent developing vs system administration on a small project

Leave a Reply