15

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?

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

2 Answers 2

11

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

4
  • 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, 2016 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, 2016 at 19:42
  • 1
    That was it! Damn, I hate when it's something as simple as that :) Thanks for your help!
    – doovers
    Oct 7, 2016 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, 2016 at 22:57
1

I faced something similar, I had all folder correctly exists but getting the same error. I get it to work by changing the "User" parameter. I set it as root and worked. Fyi.

2
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 26, 2022 at 19:49
  • Really bad for security reasons. Your daemons should never run as root, but the folders involved need to be owned by the user your daemon is running. Aug 23, 2023 at 18:19

You must log in to answer this question.

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