SECTION 05 · 25 MIN READ & EXPLORE
Keep the application running with systemd
Separate a foreground process from a service that starts on boot and restarts after failure.
Service Management (Systemd)
Objective: We need the app to run in the background, start on boot, and restart if it crashes.
The Systemd Cheat Sheet
| Command | Description |
|---|---|
systemctl start <service> |
Start a service immediately. |
systemctl stop <service> |
Stop a service. |
systemctl restart <service> |
Stop and then Start (good for config changes). |
systemctl enable <service> |
Configure service to start automatically on boot. |
systemctl status <service> |
Check if it’s running and see recent logs. |
systemctl daemon-reload |
Run this after you edit any .service file. |
journalctl -u <service> |
View the logs for a specific service. |
journalctl -xe |
View the most recent system errors (the “Why did it fail?” command). |
journalctl -f |
“Follow” the logs in real-time (like tail -f). |
Lab Task: Create the Service
Create the unit file at /etc/systemd/system/node-app.service.
[Unit]
Description=My SysAdmin Node App
After=network.target
[Service]
# The command to run the app
ExecStart=/usr/bin/node /opt/node-app/server.js
# Restart automatically if the app crashes (exit code != 0)
Restart=always
RestartSec=5
# Working directory (useful so the app knows where '.' is)
WorkingDirectory=/opt/node-app
# Standard Output/Error logging
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=node-app
[Install]
WantedBy=multi-user.target
Activate it:
systemctl daemon-reload
systemctl enable --now node-app
systemctl status node-app
Pause and predict
Does enabling a service start it immediately?
Try your own explanation first. These notes stay on this page and are not saved or graded.
Show explanation
Enabling configures boot activation. Use start separately or enable –now to do both.
Verify recovery
On the disposable VM, inspect systemctl show node-app -p MainPID, stop that process with a signal, and watch journalctl -u node-app -f. Explain why systemctl stop node-app behaves differently from an unexpected process exit.
Take a moment to explain the idea in your own words.