Skip to content

Instantly share code, notes, and snippets.

@mael
Last active June 5, 2019 09:35
Show Gist options
  • Save mael/fd342f780b0dafa2fc9289fa4c3984fe to your computer and use it in GitHub Desktop.
Save mael/fd342f780b0dafa2fc9289fa4c3984fe to your computer and use it in GitHub Desktop.
Start nodejs service on systemd raspberry pi 3b+

Creating a systemd Service File

$ cd /lib/systemd/system

$ sudo vi /lib/systemd/system/myNode.service

and put the following contents in it (example):

[Unit]
Description=Start app.js on my Node 
After=network.target

[Service]
Environment=NODE_PORT=3001
Type=simple
User=pi
ExecStart=/usr/bin/node /home/pi/myApp/app.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

Using systemctl To Control Our App

You have to do this whenever any of the service files change at all so that systemd picks up the new info.

$ sudo systemctl daemon-reload

If you want to make the application start up when the machine boots, you accomplish that by enabling it

$ sudo systemtl enable myNode.service

Other options

$ sudo systemctl [status,start,stop,restart,enable,disable] myNode.service

List

$ systemctl list-unit-files | grep enabled

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