Using systemd to run your Node.js app simplifies and streamlines the process, eliminating the need for additional scripts to manage your application.
Create the node.js server
const http = require('http');
const hostname = '127.0.0.1''; // listen on all ports
const port = 3311;
http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Node app is running!');
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
You can save this file under the path /home/node/server.js and make sure you can it run within console:
$ nodejs /home/myserver/server.js
Expected result:
Server running at http://127.0.0.1:3311/
Create the service file
Create a service file on /etc/systemd/system/
$ cd /etc/systemd/system/
$ nano node.service
File content:
[Unit]
Description=Start Node.js App
After=network.target
[Service]
ExecStart=/usr/bin/npm start
KillMode=control-group
WorkingDirectory=/home/myserver/
Restart=on-failure
[Install]
WantedBy=multi-user.target
A line-by-line explanation of the above file's content:
[Unit]
The [Unit] section provides metadata and dependencies for the unit file.
Description=Start Node.js App
A short description of the service. This will appear in systemctl commands like systemctl status node.service.
After=network.target
Specifies that this service should start only after the network.target is active. It ensures the network is up before your Node.js app starts, which is often necessary for apps relying on networking.
[Service]
The [Service] section defines how the service behaves when it starts, stops, or restarts.
ExecStart=/usr/bin/npm start
The command to execute when starting the service. In this case, it runs npm start from the Node.js application's working directory. This assumes your package.json file has a start script configured.
KillMode=control-group
Controls how processes in the service's cgroup are terminated. control-group means that systemd will kill all processes in the control group (not just the main process) when stopping the service.
WorkingDirectory=/home/myserver/
The directory where the service runs. This is typically the root directory of your Node.js application, containing files like package.json.
Restart=on-failure
Configures the service to automatically restart if it exits with a non-zero exit code (indicating failure). This improves the reliability of the service by ensuring it stays running.
[Install]
The [Install] section specifies how and when the service should be started during the system boot process.
WantedBy=multi-user.target
Indicates that this service should be started in the multi-user.target environment, which is a common system state for non-graphical, multi-user systems. This ensures the service starts automatically when the system boots into this state.
Enable the service
Enable the service to start on server boot
$ systemctl enable node.service
Start the service
$ systemctl start node.service
Verify it is running
$ systemctl status node.service
$ journalctl -u node.service
**Journalctl
A tool which interacts with the systemd journal for querying logs
& a useful journalctl command
journalctl -xfeu node.service --since "2024-11-13 00:00:00"
--since "2024-11-13 00:00:00"
With the above option of the command you can limit the logs to entries that occurred after the specified date and time (2024-11-13 00:00:00). This ensures you only see logs from a specific time frame onward.
-f: Follows the log in real time. Similar to tail -f, this allows you to see new log entries as they are written
-e: Jumps to the end of the logs, showing the most recent entries. This is useful when combined with -f
-u node.service: Filters logs for a specific systemd service (node.service in this case). Only log entries related to node.service will be shown.
Top comments (0)