Linux - Run node.js service with systemd
Node.js as a running service is becoming more and more popular these days. One of the issues many developers face is how to ensure their node.js service starts automatically, and more importantly how to keep it running should it crash.
Using systemd, which makes this process a lot simpler and more efficient, and means that we do not need another scripts to run your server node.js.
Create the node.js server
const http = require('http');
const hostname = '0.0.0.0'; // listen on all ports
const port = 3311;http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World');}).listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`);});
We will save this file (for example) as /home/myserver/server.js . Verify the node server works on terminal:
$ node /home/myserver/server.js
# or nodejs, maybe
$ nodejs /home/myserver/server.js
Output: Server running at http://0.0.0.0:3311/
Create the service file on Systemd
Create a service file on /etc/systemd/system/ or /lib/systemd/system/ (etc|lib).
$ cd /etc/systemd/system/
$ nano myserver.service
File location: /etc/systemd/system/myserver.service
[Unit]
Description=My Little Server
# Documentation=https://
# Author: Natan Cabral[Service]
# Start Service and Examples
ExecStart=/usr/local/bin/node /home/myserver/server.js
# ExecStart=/usr/bin/sudo /usr/bin/node /home/myserver/server.js
# ExecStart=/usr/local/bin/node /var/www/project/myserver/server.js# Options Stop and Restart
# ExecStop=
# ExecReload=# Required on some systems
# WorkingDirectory=/home/myserver/
# WorkingDirectory=/var/www/myproject/# Restart service after 10 seconds if node service crashes
RestartSec=10
Restart=always
# Restart=on-failure# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-my-server-example# #### please, not root users
# RHEL/Fedora uses 'nobody'
# User=nouser
# Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
# Group=nogroup# variables
Environment=PATH=/usr/bin:/usr/local/bin
# Environment=NODE_ENV=production
# Environment=NODE_PORT=3001
# Environment="SECRET=pGNqduRFkB4K9C2vijOmUDa2kPtUhArN"
# Environment="ANOTHER_SECRET=JP8YLOc2bsNlrGuD6LVTq7L36obpjzxd"[Install]
WantedBy=multi-user.target
Enable the service
Enable the service on boot
$ systemctl enable myserver.service
Start the service
$ systemctl start myserver.service
$ systemctl restart myserver.service
$ systemctl status myserver.service
Verify it is running
$ journalctl -u myserver.service
Reload all services if you make changes to the service
$ systemctl daemon-reload
$ systemctl restart myserver.service
List process
$ ps -ef | grep myserver.js
Maybe we need permissions
$ chmod +x /etc/systemd/system/myserver.service
$ chmod 664 /etc/systemd/system/myserver.service
To kill process if you need try
$ kill -9 {numberPID}
Now finally we can test how systemd restarts the process if it unexpectedly dies.
;) have fun!
Names to remember:
- systemctl — system control
- systemd — system manager standard
- ps — process
- chmod — change the mod