This is a tutorial on how to configure systemd unit to run Nodejs application.

What is a process manager

So you make a Node.js application and want to run it on a production web server. How to do that? You can simply run a command like node yourapp.js in a bash terminal. But what if the app crashes? In that case, to automatically restart on crashes, you need a process manager. It will control your Node.js application and provide you the ability to monitor it.

The most well-known Node.js process manager is a pm2. But if you use modern Linux, you already have a systemd process manager without any additional installation.

What is a systemd

systemd is an initialization system and service manager. It is using to run and manage services and daemons.

  • A service is a program that responds to requests from other programs.
  • A daemon is a background and non-interactive program.

In systemd services are called units and configured using unit files. Each unit file should have a suffix service. So let’s do some practice and create a unit file.

How to create a unit file

Create a new file and open it for editing. I am using a CLI nano text editor. You can use any.

sudo nano /etc/systemd/system/my_node_app.service

Then put the following content to this file. I will describe the main points below.

Description=my_node_app.js - app description here

ExecStart=/usr/bin/node /home/my_node_app/app.js


So there are main points in this file:

  • “After” tells systemd when to start your Node.js app after the machine boots. means systemd should wait until the main networking functionality is loaded. We need that because we can’t bind Node’s app port until the network is up and running.
  • “Type=simple” tells systemd how our application loads. systemd just fork the process and run it.
  • “Restart=on-failure” tells by itself. If there is any application crash, it will restart automatically.

Read the documentation if you need other options or more details.

Apply configuration and start service

The next step is to apply this unit. There is systemctl command for management systemd. Let’s reload systemd daemon at first. You have to do this every time you change service files.

sudo systemctl daemon-reload

Then start the newly created service using the command below.

sudo systemctl start my_node_app

Some useful systemctl commands

To start a systemd service use the start command:

sudo systemctl start application.service

And a short variant, without service suffix:

sudo systemctl start application

To stop the service use this command:

sudo systemctl stop application.service

The next two commands allow you to restart or reload the service. Reload tells the service to reload its configuration files. Restart will shut down the application and then restart it.

sudo systemctl reload application.service
sudo systemctl restart application.service

To start the service automatically at the boot, you must enable it, using the next command:

sudo systemctl enable application.service

Enabling a service does not start it. This command will create a symbolic link from the system’s copy of the service file, usually located in /lib/systemd/system or /etc/systemd/system, into the location on a disk where systemd looks for autostart files, for example,/etc/systemd/system/

To disable the service from starting automatically, use the next command:

sudo systemctl disable application.service

The next command will show you the status of your application:

sudo systemctl status application.service

And the last command here is to see the service logs, using journalctl.

sudo journalctl -u application


It was the simplified basics to start using systemd as a process manager for your Node.js application.
Read journalctl or systemctl documentation to know more details.