Extremely minimal wrapper around
- any Linux distribution that supports systemd
- C/C++ tool stack (GCC/Clang, etc...)
- Node.js >=
Firstly you need some systemd development files, on Ubuntu these can be installed via:
$ sudo apt install libsystemd-dev
$ npm install --save sd-notify
const notify =// call notify after some async start up process// such as in the `http` or `express` listen callbackapp
.ready() will inform systemd that the process has started, when using
notify type in a service definition file, eg:
[Unit]Description=Simple notifying service[Service]Environment="NODE_ENV=production"Type=notifyExecStart=/usr/sbin/simple-notifying-serviceTimeoutStartSec=30Restart=always[Install]WantedBy=multi-user.target
In the service file add
n is the amount of seconds
stop (or restart) the service if there is no contact.
...and in Node, you can call the native method
.watchdog() directly in a
setInterval or any other mechanism
depending on what kind of application you are developing, or you can use the helper function
const notify =app
...above the number supplied to the
startWatchdogMode method is the amount of milliseconds
we want to ping
systemd, in the example this is 200ms less than the 3 seconds set in the
service file. Due to the event loop there is no guarantee the
setInterval underneath will
fire exactly 2800ms, this will change depending on how many functions are being called in the process,
though this has a nice side effect, as if the process gets that busy, that blocked,
systemd will kill it
(and restart it with the
Restart= config set); and in the context of having multiple processes being load
balanced with Nginx (as an example) and across multiple machines, ensures that no one process is blocking
for any significant amount of time.
You can also check if the process was called by systemd with Watchdog mode
watchdogInterval() which returns the amount of milliseconds
watchdog has been set to, or
0 if it has not been set:
...this way the Node process will behave in the correct manner in either situation.
You can also send some status string to systemd, which will append to the service's log.
const notify =// ...notify// ...
...then, for example:
$ journalctl -u node-status ... Apr 22 17:29:41 lenovo node: (8275) listening on 8000 Apr 22 17:29:41 lenovo systemd: Started Express Node.js. Apr 22 17:35:50 lenovo node: send some status to systemd ...