To start a systemd
service manager daemon, write a service file. For example, create a file /etc/systemd/system/myservice.service
.
If your server.js
script is shebanged (#!/usr/bin/env node
on the first line) and the system can find node
on the path, this works:
[Unit]
Description=myservice-description
After=network.target
[Service]
ExecStart=/opt/myservice-location/src/node/server.js --args=here
Restart=always
User=me
Group=group
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/opt/myservice-location
[Install]
WantedBy=multi-user.target
Or, if you don't shebang the file, consider adding the absolute path to the node
executable that you want to use and prepend that to the ExecStart=
attribute of the service file.
...
ExecStart=/root/.nvm/versions/node/v16.15.1/bin/node ...
...
Remember to update the service manager daemon after every change to the myservice.service file.
$ systemctl daemon-reload
Then start the service running and enable the service to start at boot.
$ systemctl start myservice
$ systemctl enable myservice