Skip to content

Instantly share code, notes, and snippets.

@maliarslan
Last active May 26, 2024 10:19
Show Gist options
  • Save maliarslan/a50c5f335a18ebb31068663cabec98af to your computer and use it in GitHub Desktop.
Save maliarslan/a50c5f335a18ebb31068663cabec98af to your computer and use it in GitHub Desktop.
Run Node.js application as service on Ubuntu 16.04

Assuming that you have already installed Node.js on your system. If you haven't visit https://nodejs.org/en/download/package-manager/

First we need to create a service file into /lib/systemd/system to introduce our service.

So, with your favorite editor, open up a new file there with;

sudo nano /lib/systemd/system/mygreatestapp.service

Put the following contents in it;

[Unit]
Description=mygreatest node.js app to make the world great again!
Documentation=https://www.mygreatestapp.com
After=network.target 

[Service]
Type=simple
User=username
ExecStart=/usr/bin/node /home/username/mygreatestapp/mygreatestapp.js
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • After=network.target means our service will start after the network is up and running
  • Type=simple specify that how our app launch itself. Simple means it won't drop user priviliges
  • User=username tells that our app will run as the unprivileged user unless you want to run it by root
  • ExecStart=/usr/bin/node /home/username/mygreatestapp/mygreatestapp.js tells systemd the location of our app and what command it should run
  • Restart=on-failure clearly says under what condition system restart our service. But if you stop the service it won't restart itself.
  • WantedBy=multi-user.target specify how our service should be enabled

Now you have your service file in /lib/systemd/system folder.

Whenever you change a service file, systemd has to know it so that it no longer attempts to reference these files and reverts back to using the system copies. . You can do this by typing:

sudo systemctl daemon-reload

What's coming after is to launch our app with:

sudo systemctl start mygreatestapp

You can use following commands to check service status and stop it

sudo systemctl status mygreatestapp sudo systemctl stop mygreatestapp

Following commands can be used to do same

service mygreatestapp start service mygreatestapp status service mygreatestapp stop

There is a good trick for some situations (e.g. working directory issues if you use modules like TesseractJs) to use in ExecStart is using bash script.

Open an .sh file to write our script

sudo nano /home/username/myrunscript.sh

Than put the script you want to run in it

cd /home/username/mygreatestapp; /usr/bin/node mygreatestapp.js

You have to make your script file executable. To do so:

sudo chmod +x /home/username/myrunscript.sh

Now you have your executable script and it can be used in ExecStart as seen below

ExecStart=/bin/bash /home/username/myrunscript.sh

As mentioned above you have to let systemd know if you change anything in there

sudo systemctl daemon-reload

@Disclosure
Copy link

I assume in User=username username stands for the name of the user that should be running the service, like www-data?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment