1

I am trying to run a Node.js app as a service in Debian 9. So, following some tutorials, I've made a service like that.

[Unit]
Description=bumer API Http Server
[Service]
PIDFile=/tmp/bumer-service.pid
User=root
Group=root
Restart=always
KillSignal=SIGQUIT
WorkingDirectory=/root/bumer/
SyslogIdentifier=bumer
ExecStart=/root/bumer/app.js
[Install]
WantedBy=multi-user.target

My app has #!/usr/bin/env node as its first line and it runs pretty well if I run the command /root/bumer/app.js. The problem is that when I start the service I was getting the following error:

main process exited, code=exited, status=127/n/a

So, I tried to change the first line of the app.js file to the node's path instead of the word "node" #!/usr/bin/env /usr/local/lib/nodejs/node-v8.11.4/bin/node and now it's running fine. I guess the service is not reading the PATH variable and I need some help to realize why it doesn't.

(my PATH is right and when I run "type node" the result is OK).

root@bumer:~# type node
node is /usr/local/lib/nodejs/node-v8.11.4/bin/node

(I am logging the variable PORT which I set on ~/.bashrc on the app.js but it cannot read as well, it logs undefined).

0

1 Answer 1

3

Services do not run in login session contexts.

Your PATH is not right, and the value of PATH in a shell in a login session is completely irrelevant, as are your shell's startup scripts.

One of the features of service management systems (in general, not limited to systemd) is that they start all services based upon a single uniform environment, as modified only by elements of the service definition (whatever that is). It is nothing to do with how login shells set up user environments for login sessions. (Not, it is not root's login session environment either.)

In systemd's case, your service definition is the service unit file, and unless you modify PATH in that unit it will be whatever default value that all services are started with. In systemd's case, this is documented; and /usr/local/lib/nodejs/node-v8.11.4/bin/ is not on the list.

If you want PATH to be something other than the all-serviecs default, you need to modify your service unit with Environment= settings to change it.

Further reading

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .