Deploying Node Services under Systemd

Systemd

The current linux init and daemon management susbsystem

Runs as Process #1

Systemd

  • Boot Loader
  • Daemon Management
  • And so Much More

SystemD

Service Files

SystemD

Based on ini/Desktop File Format

Service Unit Files

[Unit]
Description=Packet Filtering Framework
Before=network-pre.target
Wants=network-pre.target

[Service]
Type=oneshot
ExecStart=/usr/bin/iptables-restore /etc/iptables/iptables.rules
ExecReload=/usr/bin/iptables-restore /etc/iptables/iptables.rules
ExecStop=/usr/lib/systemd/scripts/iptables-flush
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

So what about nodejs

[Unit]
Description=My Companies API
Before=network-pre.target
Wants=network-pre.target

[Service]
Environment=PORT=22434
ExecStart=/usr/bin/node /srv/api/server.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

Multiple Instances

[Unit]
Description=My Companies API
Before=network-pre.target
Wants=network-pre.target

[Service]
Environment=PORT=%i
ExecStart=/usr/bin/node /srv/api/server.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

What About Nodemon?

LOL That's a systemd.path

deck

By Matt Sprague