I created a systemd
service file in directory /lib/systemd/system/
to launch nodejs
as a service. The problem is that the file contains hardcoded path to nodej
in ExecStart
field. It means that any time I update nodejs
I will have to manually change this service file. Is there a way to use which node
command in the file below to automatically set the path?
[Unit]
Description=node-server-1
After=network.target
[Service]
Environment=NODE_PORT=3001
Type=simple
User=manid
ExecStart=/home/manidos/.nvm/versions/node/v14.15.1/bin/node /home/manidos/node-nginx/index.js
Restart=on-failure
[Install]
WantedBy=multi-user.target
You can run a simple bash script within the ExecStart
line:
ExecStart=/bin/bash -c '$$(/usr/bin/which node) /home/manidos/node-nginx/index.js'
Basically, this is running the which
command in a subshell and passing its stdout to be run by the outer script. Double-dollars are needed to use this particular syntax in a systemd unit file, as documented in the systemd.service manpage.