NG.
NG.

Reputation: 469

Ubuntu Nodejs systemd service failure

I have the current latest node v8.6 installed on my Ubuntu 16.04 VPS. The node app.js is in var/www/back-end.

Now I am trying to run it as a systemd service, but that fails:

Process: 8583 ExecStart=/usr/bin/nodejs /var/www/back-end/app.js (code=exited, status=1/FAILURE)

in other questions I read about the path to the node executable maybe being wrong, but I checked with 'which nodejs' and that gave back '/usr/bin/nodejs'. I also checked the permissions for var/www/back-end and they are set correctly to admin.

In another question I read that apt-get calls it nodejs because of a conflict and that a symlink should be created, however I was not convinced. I should note that I need the latest version of node because of it's features, so downgrading is not an option.

Any idea what could be wrong? Or is there some logfile in which I can find a better error so I know what is causing this?

This is my .service file:

[Unit]
Description=Node_API
After=mongodb.service

[Service]
ExecStart=/usr/bin/nodejs /var/www/back-end/app.js
Restart=always
RestartSec=5
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodeapi
User=admin
Group=admin
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production PORT=3000
WorkingDirectory=/var/www/back-end

[Install]
WantedBy=muti-user.target

Upvotes: 1

Views: 1174

Answers (1)

Mark Stosberg
Mark Stosberg

Reputation: 13391

You didn't mention testing that the command runs correctly when run from the CLI as the admin user:

 cd /var/www/back-end && PATH=/usr/bin:/usr/local/bin NODE_ENV=production PORT=3000 /usr/bin/nodejs /var/www/back-end/app.js

You also didn't mention checking the permissions on the parent directions of /var/ and /www, just the script itself. These will also need to allow the admin user to have the execute permission, as explained more here.

Finally, your output indicates that the script did start but then exited, so you should have the ability to add diagnostics to your script to better understand why it's not starting. According to your configuration, you've sent the output of the script to syslog, you should look for the logs there. You can add a console.log() as soon as the script starts, and continue to add more to confirm at what step it's dying.

Also, you have a typo here: WantedBy=muti-user.target. That should be "multi-user.target".

You can also use systemd-analyze verify /path/to/your/file.service to check your file for syntax correctness.

Upvotes: 3

Related Questions