11

I attempted to follow this guide to run a Node application as a service. However, it is failing to start, with exit code 127. Is there any way to fix this?

This is the journal.

sudo  journalctl --follow -u serviceName
-- Logs begin at Tue 2017-08-08 16:27:10 GMT. --
Aug 08 17:06:57 raspberrypi systemd[1]: Started serviceName.
Aug 08 17:06:57 raspberrypi app.js[7234]: [46B blob data]
Aug 08 17:06:57 raspberrypi systemd[1]: serviceName.service: main process exited, code=exited, status=127/n/a
Aug 08 17:06:57 raspberrypi systemd[1]: Unit serviceName.service entered failed state.
Aug 08 17:06:57 raspberrypi systemd[1]: serviceName.service holdoff time over, scheduling restart.
Aug 08 17:06:57 raspberrypi systemd[1]: Stopping serviceName...
Aug 08 17:06:57 raspberrypi systemd[1]: Starting serviceName...
Aug 08 17:06:57 raspberrypi systemd[1]: serviceName.service start request repeated too quickly, refusing to start.
Aug 08 17:06:57 raspberrypi systemd[1]: Failed to start serviceName.
Aug 08 17:06:57 raspberrypi systemd[1]: Unit serviceName.service entered failed state.

This is the serviceName.service.

[Unit]
Description=ServiceName
After=network.target

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

[Install]
WantedBy=multi-user.target

This is at the top of my app.js.

#!/usr/bin/env node
Chris Talman
  • 211
  • 1
  • 2
  • 4

5 Answers5

6
ExecStart=/home/pi/projects/ServiceName/app.js

This is telling systemd to run app.js directly. Is this .js file directly executable? If not, the shell will throw an exit code 127 - "Unknown command".

DopeGhoti
  • 73,792
  • 8
  • 97
  • 133
  • 1
    According to the guide I linked in the question, the shebang at the top of `app.js` should make it executable, because it specifies Node as the interpreter. – Chris Talman Aug 08 '17 at 19:22
  • 1
    I believe @DopeGhoti was referring to the executable permission - as in, "read-write-executable" permissions set per file. – SEoF Feb 20 '18 at 11:44
  • "If not, the shell will throw an exit code 127" -- whatever the exact reason for the 127, systemd does not use the shell to start services. – phemmer May 29 '18 at 12:10
2

127 is a command not found.

Make sure root user has access to node binary else change following lines with the user for which you have installed node

User=root
Group=root

Else Try following

[Unit]
Description="ServiceName"
After=network.target

[Service]
ExecStart=path_to_node/node /home/pi/projects/ServiceName/app.js
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10

# Output to syslog
StandardOutput=syslog
StandardError=syslog
#Change this to find app logs in /var/log/syslog
SyslogIdentifier=nodejs-api
# Followig will require if you are using the PORT or Node from Envirnoment
Environment=NODE_ENV=production PORT=3000

[Install]
WantedBy=multi-user.target

Once your server machine is up & you are not able to access server, you troubleshoot by checking logs from /var/log/syslog by the following command

sudo grep "nodejs-api" /var/log/syslog

Start on boot: sudo systemctl enable rocketch

Josip Rodin
  • 1,119
  • 8
  • 14
Chandani Patel
  • 208
  • 3
  • 9
2

To debug just run this (substuting the <> parts with the value from your systemd unit file:

sudo runuser -l <User> -g <Group> -c "cd <WorkingDirectory> && <PATH> <ExecStart>"

The issue is the given User cannot access the ExecStart within the PATH you have set.

If anybody is still stuck I've written a step by step guide to: debugging systemd

Gerry
  • 1,030
  • 9
  • 9
1

For them who are using NVM. try this

ExecStart=/home/<you_username>/.nvm/versions/node/<node_version>/bin/node /home/<you_username>/.nvm/versions/node/<node_version>/bin/verdaccio --config /home/<you_username>/.config/verdaccio/config.yaml
9me
  • 111
  • 1
0

Hey for anyone interested, I had this issue and tried everything under the sun, but in the end upgrading from Node 6 to Node 12 fixed the issue.