@KizaHere

Systemd cheat sheet

How to create a systemd service:

View existing systemd services:

ls /etc/systemd/system/

Make a serviceName.service file with the template below in the folder:

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

Common commands:

sudo systemctl start example
sudo systemctl status example
sudo systemctl stop example
sudo systemctl restart example

### ENABLE SERVICE ### !!!IMPORTANT!!! This enables it to run on boot

sudo systemctl enable example

Template:
[Unit]
Description=

[Service]
ExecStart=/usr/bin/node /home/kiza/AppName/server/server.js
User=kiza
Group=kiza
Restart=always
RestartSec=10
Environment=NODE_ENV=production
WorkingDirectory=/home/kiza/AppName/

[Install]
WantedBy=multi-user.target

See logs with:

sudo journalctl -u example

Last 100 lines:

sudo journalctl -u example -n 100 --no-pager

Sources:

https://stackoverflow.com/questions/4018154/how-do-i-run-a-node-js-app-as-a-background-service/29042953#29042953
https://www.axllent.org/docs/view/nodejs-service-with-systemd/