I typically have a number of smallish programs I want launched as daemons.
Getting those set up nicely typically requires root privileges, startup-scripts, etc.
which I find cumbersome. Instead of /etc/init.d and friends, I use a lovely
program called Supervisord. I think of Supervisord as my personal
init: it launches programs, monitors them for failure, logs their output, and restarts them.
Furthermore, it is trivial to set up new “daemons”, either manually
or via, say Ansible (which even has a module specifically for Supervisord).
The Supervisord package has Web and XML-RPC interfaces which can be disabled if you don’t need them.
After installing Supervisord (as root), I can choose to configure it in such a way as that it can be controlled by a non-privileged user (the configuration below does that: Supervisord will make my user owner of its socket, which allows me to control it without using sudo. (As you know, using sudo can be hazardous to my health … ;-)
Once Supervisord is running, I can create a small configuration file (ini -style) for a new program I want to run under its control. Here’s an example for mqttwarn:
If supervisord is running as root (which it typically does, but need not), I can configure the user under which my program is launched, set up specific environment variables, define expected exit status codes, whether or not to restart the program on exit, stdout redirection, logfile rotation, and a slew of other things.
I then use supervisorctl to “talk” to the supervisord process, and tell it to re-read its configuration and launch or stop processes depending on the state of the configuration:
Programs can be stopped or restarted. reread reloads the configuration without stopping or starting processes, and update will relaunch processes with changed configurations. So, if you want to pick up changes to config files with minimal disruptions, use reread followed by update.
If Supervisord’s XML-RPC interface is enabled, I can use the interface to check on Supervisord with, say, Nagios or Icinga. For a very simple example, this Python script connects to Supervisord and obtains information on whether WSS is running or not:
The above program produces output like this:
Supervisord is highly recommended; I rely on it on my workstation and on servers.