This systemd unit file autostarts your NodeJS service using npm start
. Place it in /etc/systemd/system/myapplication.service
(replace myapplication
by the name of your application!)
[Unit] Description=My application [Service] Type=simple Restart=always User=nobody Group=nobody WorkingDirectory=/opt/myapplication ExecStart=/usr/bin/npm start [Install] WantedBy=multi-user.target
Replace:
/opt/myapplication
by the directory of your application (wherepackage.json
is located)User=nobody
andGroup=nobody
by the user and group you want to run the service as- Optionally you can add a custom decription instead of
Description=My application
Then enable start at boot & start right now: (Remember to replace myapplication
by the name of the service file you chose!)
sudo systemctl enable --now myapplication
Start by
sudo systemctl start myapplication
Restart by
sudo systemctl restart myapplication
Stop by
sudo systemctl stop myapplication
View & follow logs:
sudo journalctl -xfu myapplication
View logs in less
:
sudo journalctl -xu myapplication