@KizaHere

Systemd cheat sheet

How to create a systemd service:

Recent services

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/{{name}}.service

Common commands:

{{sudo}} systemctl start {{name}}
{{sudo}} systemctl status {{name}}
{{sudo}} systemctl stop {{name}}
{{sudo}} systemctl restart {{name}}

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

{{sudo}} systemctl enable {{name}}

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 {{name}}

Last 100 lines:

{{sudo}} journalctl -u {{name}} -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/