gitmyhub

node-systemd-demo

JavaScript ★ 20 updated 10y ago

run a Node app as a daemon with Systemd

A beginner-friendly guide showing how to run a Node.js web server as a background Linux service using systemd, so it keeps running after reboot and restarts on crash.

Node.jsSystemdLinuxsetup: moderatecomplexity 3/5

This repository is a beginner-friendly guide to running a Node.js web server as a background service on Linux, so it stays running even after you close your terminal or reboot your computer. Instead of manually starting and stopping your app, the system handles it automatically.

The repo includes a simple example: a Node app that listens on port 5000 and returns "Hello World" when you visit it. The main trick is creating a service configuration file that tells Linux (through a tool called Systemd) how to launch your app, what user to run it as, and what to do if it crashes. The instructions walk you through editing this file with your own file paths and usernames, copying it to the system directory, and then using a few commands to start and monitor the service. Once set up, you can check logs, restart the app, or even make it automatically start on boot.

The second half explores a more advanced feature called socket activation. Instead of your Node app constantly listening for connections, Systemd listens on the port instead. When a request arrives, Systemd wakes up your app, hands it the connection, and lets it handle the request. Once there's been no activity for a while, Systemd shuts the app down to save resources. This is useful if you have many services or want to save memory on a small server—your app only runs when actually needed.

You'd use this if you're deploying a Node app to a Linux server and want it to behave like a professional service: restarting on failure, logging to the system journal, starting automatically on reboot, and being manageable from the command line. It's a practical alternative to other approaches and shows the Linux-native way to do this, without needing extra tools or frameworks.

Where it fits