I'm trying to automatically run the following command on system boot with systemd.

/usr/bin/node /var/www/html/rest-api/dist/index.js

I have verified the command works by running it manually but when I try to initiate it with a rest.service file I'm getting errors.

rest.service:

[Unit]
Description=REST API
After=network.target

[Service]
ExecStart=/usr/bin/node /var/www/html/rest-api/dist/index.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/rest-api/dist

[Install]
WantedBy=multi-user.target

journalctl -u rest-api output:

Where rest.service User & Group = nobody:

Oct 06 02:10:28 ip-172-31-26-208 systemd[7172]: rest-api.service: Failed at step GROUP spawning /usr/bin/node: No such process
Oct 06 02:10:28 ip-172-31-26-208 systemd[1]: rest-api.service: Main process exited, code=exited, status=216/GROUP
Oct 06 02:10:28 ip-172-31-26-208 systemd[1]: rest-api.service: Unit entered failed state.
Oct 06 02:10:28 ip-172-31-26-208 systemd[1]: rest-api.service: Failed with result 'exit-code'.
Oct 06 02:10:29 ip-172-31-26-208 systemd[1]: rest-api.service: Service hold-off time over, scheduling restart.
Oct 06 02:10:29 ip-172-31-26-208 systemd[1]: Stopped REST API. 

Where rest.service User & Group = root:

Oct 06 02:20:11 ip-172-31-26-208 systemd[1]: Started REST API.
Oct 06 02:20:11 ip-172-31-26-208 systemd[1]: rest-api.service: Main process exited, code=exited, status=200/CHDIR
Oct 06 02:20:11 ip-172-31-26-208 systemd[1]: rest-api.service: Unit entered  failed state.
Oct 06 02:20:11 ip-172-31-26-208 systemd[1]: rest-api.service: Failed with  result 'exit-code'.
Oct 06 02:20:12 ip-172-31-26-208 systemd[1]: rest-api.service: Service hold-off > time over, scheduling restart.
Oct 06 02:20:12 ip-172-31-26-208 systemd[1]: Stopped REST API.

Any ideas how to fix?

  • Adding a short description of what are you doing/trying to achieve might be helpful for people reading this. – mook765 Oct 6 '16 at 7:10
  • @mook765 Updated question to give more detail :) – doovers Oct 7 '16 at 7:26
up vote 2 down vote accepted

code=exited, status=200/CHDIR is your key error message.

It indicates that /var/www/rest-api/dist doesn't exist or is not accessible at the time your service is attempting to run.

If it's mounted over the network, After=network.target does not necessarily mean that any given network mount has been mounted. It's even possible that a slow to start local drive might not be mounted yet. To see if you're experiencing a race condition, try adding a delay or possibly using RequiresMountsFor=, or even ConditionPathExists=.

source: https://www.freedesktop.org/software/systemd/man/systemd.unit.html

  • I updated my question to give more detail. I have verified the ExecStart command does in fact work when run manually and also systemctl start rest-api is yielding the same result so you're suggestion that a slow local drive might be at fault would not seem to hold true unfortunately. Any other thoughts as to what may be the problem here? – doovers Oct 7 '16 at 7:30
  • 1
    Doovers, running as root, I'm able to duplicate your error only by setting WorkingDirectory= to a directory that doesn't exist or can't be reached at the time the service is run. If you're not network mounting the directory and able to duplicate the problem long after boot then those two things don't apply. Something is wrong with the path, /html/ is present in one path and missing from another, so that is suspect but could be correct. – Chisight Oct 7 '16 at 19:42
  • That was it! Damn, I hate when it's something as simple as that :) Thanks for your help! – doovers Oct 7 '16 at 22:51
  • 3
    Although I'd like to know why I can't run it as nobody instead of root. Any ideas on that? – doovers Oct 7 '16 at 22:57

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.