Manage a Node.js service with systemd

Manage Node.js with a service unit, inspect status and logs, and verify failure recovery and startup at boot.

🎯 Introduction

The previous article started an HTTP service manually with node. Who starts it again after a reboot or an unexpected exit? systemd starts and manages services on Ubuntu. Give it the Node.js startup command and restart policy to automate startup and recovery.

This article manages the same server.mjs, uses systemctl to start and control it, and verifies failure recovery and startup at boot.


🧱 Put the Node.js startup command in a service unit

systemd represents each managed service as a service unit. A .service file describes its command, execution account, and behavior after the process exits. systemctl sends instructions to systemd and queries its state.

Reuse the program and current account from the previous article. First obtain the account name and entry file's absolute path for the configuration.

whoami
realpath -e "$HOME/linux-lab/app/server.mjs"

You can test as root on the course practice machine. If whoami prints root, use User=root below and omit sudo from commands if desired.

Create the configuration with sudo vim /etc/systemd/system/linux-notes.service. Replace User and the entry file path in ExecStart with the values you found. Keep the Node.js path from the course installation. Packages and environment variables

linux-notes.service
[Unit]
Description=Node.js HTTP Service

[Service]
Type=exec
User=learner
ExecStart=/opt/linux-course/runtime/bin/node "/home/learner/linux-lab/app/server.mjs"
Environment=PORT=3000
Restart=on-failure
RestartSec=3s

[Install]
WantedBy=multi-user.target
SettingPurpose in this example
DescriptionShows a description in the service status.
Type=execMarks the service started once systemd successfully executes Node.js.
UserSelects the account running Node.js.
ExecStartSpecifies the startup command and entry file.
Environment=PORT=3000Passes port 3000 to the application as an environment variable.
Restart=on-failureAttempts a restart after an abnormal Node.js exit.
RestartSec=3sWaits 3 seconds before an automatic restart.
WantedBy=multi-user.targetWhen enabled, adds the service to the set started during normal Ubuntu boot.

See the official manuals for all configuration options. systemd.service · systemd.exec


▶️ Start the service and check its endpoint

If the manually started process is still running, stop it as described previously to free port 3000. Run daemon-reload to read the new unit file, then start to start the service. The filename linux-notes.service is also the unit name used in commands. Process management

sudo systemctl daemon-reload
sudo systemctl start linux-notes.service
systemctl status linux-notes.service --no-pager

active (running) means the Node.js process is running. Request the health endpoint too: HTTP 200 and {"status":"ok"} confirm that the service responds.

curl -i http://127.0.0.1:3000/health

If startup fails or the endpoint remains inaccessible after a short wait, inspect the unit's recent logs with journalctl.

sudo journalctl -u linux-notes.service -b -n 20 --no-pager

🧪 Verify recovery after an unexpected process exit

First record MainPID, the PID of the Node.js process currently managed by systemd. Use it afterward to confirm that systemd started a new process.

systemctl show -p MainPID --value linux-notes.service

If the output is 0, check service status first. Once it is running, forcibly terminate its main process with SIGKILL on the course practice machine to simulate a sudden exit. The endpoint will be briefly unavailable.

sudo systemctl kill --kill-whom=main --signal=SIGKILL linux-notes.service

Wait a few seconds, then inspect the PID, logs, and endpoint again.

systemctl show -p MainPID --value linux-notes.service
sudo journalctl -u linux-notes.service -b -n 20 --no-pager
curl -i http://127.0.0.1:3000/health

In the logs, status=9/KILL records termination of the old process, and Scheduled restart job records the scheduled restart. A changed PID and a 200 response confirm that the new process took over.


🔁 Enable and verify startup at boot

start runs the service now. enable configures it to start on future boots. Enable it, then verify that the setting was recorded.

sudo systemctl enable linux-notes.service
systemctl is-enabled linux-notes.service

After seeing enabled, run sudo reboot on the dedicated practice machine to test it. Once the machine is back online, log in and check the status and endpoint directly.

systemctl status linux-notes.service --no-pager
curl -i http://127.0.0.1:3000/health

active (running) and HTTP 200 verify that the service actually started at boot.


❓ Knowledge check

Does stopping a service with systemctl stop trigger failure recovery?

An intentional stop leaves the service stopped. Run sudo systemctl start linux-notes.service to start it again. If enabled, it will also start at the next boot.


🧪 Exercise

  1. Change Environment=PORT=3000 to Environment=PORT=3001 in linux-notes.service.
  2. Run sudo systemctl daemon-reload to read the change, then sudo systemctl restart linux-notes.service to give the new process the updated port. Request http://127.0.0.1:3001/health and confirm a 200 response.
  3. Explain what each command did. Restore port 3000, reload and restart, and confirm that http://127.0.0.1:3000/health returns 200 again.