Linux - Run node.js service with systemd

Download Repository Files

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

about permissions

To kill process if you need try

$ kill -9 {numberPID}

Now finally we can test how systemd restarts the process if it unexpectedly dies.

Download Repository Files

;) have fun!

Names to remember:

  • systemctlsystem control
  • systemdsystem manager standard
  • ps process
  • chmodchange the mod

--

--

--

Full Stack Developer | Dev Java, Node.js, TypeScript, React.js, Vue.js, Express.js, Next.js, Rest API, Laravel, Databases, MongoDB, Unix distro and Open Source

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to change checkbox color and cursor color on clicking on the same in android?

Understand JavaScript Array Methods Like a Pro

Implement basic Video Calling in React using AgoraWebSDK NG

While loops in Javascript

useState, this very minute!

Insecure implementation of CryptoJS encryption (Part II)

Optimize images with Sanity headless CMS

Running WASI in Javascript with Wasmer-JS

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Natan Cabral

Natan Cabral

Full Stack Developer | Dev Java, Node.js, TypeScript, React.js, Vue.js, Express.js, Next.js, Rest API, Laravel, Databases, MongoDB, Unix distro and Open Source

More from Medium

What happens from typing in a URL to displaying a website? (Part 1 — DNS cache and DNS lookup)

OUR OWN SERVER!

Version Control

Recap: JWT and Cookies for Web Authn