Put this content:
[Unit] Description={name} [Service] Type=simple User={user} ExecStart=/usr/bin/node {full-path-to-script}.js [Install] WantedBy=multi-user.target
… somewhere as {name}.service
, where:
{name}
is the name of the service{user}
is the name of the user to run the script (optional)
… then:
- run:
this sudo ln -s /{full_path}/{name}.service /lib/systemd/system/{name}.service
- then this:
sudo systemctl daemon-reload
- then this:
sudo systemctl enable {name}.service
- and finally this:
sudo systemctl start {name}
How does it work?
- It starts the service on boot (see
WantedBy
section). - Using
ExecStart
command. Important: we specify the full path tonode
SystemD
remembers thePID
of the new process and considers the service is ongoing until the process is died.- So any subsequent
systemctrl start {name}
won’t do anything if the previous process is alive. - This behavior is determined by
Type=Simple