countxerox
Posts: 14
Joined: Fri Sep 08, 2017 4:06 pm

daemonizing node js

Tue Sep 12, 2017 8:14 pm

Hi, so far I've been running the node app like ...

Code: Select all

node app.js
and that works but the command is blocked and if I log off the pi the app dies. So I'm looking for ways to run it in the background?

I found this post and tried to set up a service...

sudo nano /etc/systemd/system/myapp.service

Code: Select all

[Unit]
Description=My app

[Service]
ExecStart=/home/pi/proj/app.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/home/pi/proj

[Install]
WantedBy=multi-user.target
but the service won't start.

sudo systemctl daemon-reload
sudo systemctl start myapp
journalctl -u myapp

Code: Select all

Sep 12 19:45:46 raspberrypi systemd[14880]: myapp.service: Failed at step GROUP spawning /home/pi/proj/app.js:
Sep 12 19:45:47 raspberrypi systemd[1]: myapp.service: Main process exited, code=exited, status=216/GROUP
Sep 12 19:45:47 raspberrypi systemd[1]: myapp.service: Unit entered failed state.
Sep 12 19:45:47 raspberrypi systemd[1]: myapp.service: Failed with result 'exit-code'.
Sep 12 19:45:47 raspberrypi systemd[1]: myapp.service: Service hold-off time over, scheduling restart.
Sep 12 19:45:47 raspberrypi systemd[1]: Stopped My app.
Is this the right way to run a node js server?
I can see there's a problem with the group, I don't know what this means?

countxerox
Posts: 14
Joined: Fri Sep 08, 2017 4:06 pm

Re: daemonizing node js

Tue Sep 12, 2017 10:16 pm

OK it works now, I made some changes to myapp.service...

Code: Select all

[Unit]
Description=My app

[Service]
ExecStart=/usr/local/bin/node /home/pi/proj/app.js
Restart=always
RestartSec=10
#User=nobody
#Group=nobody
#Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production PORT=5000
WorkingDirectory=/home/pi/proj

[Install]
WantedBy=multi-user.target
it seems like /usr/local/bin/node should be defined with Environment= somehow?

Heater
Posts: 7574
Joined: Tue Jul 17, 2012 3:02 pm

Re: daemonizing node js

Tue Sep 12, 2017 11:58 pm

I think it's fine to use the full path to your node executable like that.

I have many versions of node on my machines installed with nvm. I have the full path to the node version I want to use in my service files.

Return to “Other languages”

Who is online

Users browsing this forum: No registered users and 2 guests