I have the following service file:
[Unit]
Description=MyApp
After=syslog.target network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
WorkingDirectory=/opt/nodejs-sites/MyApp
ExecStart=/usr/bin/npm start
Environment=NODE_ENV=development
User=root
Group=root
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=MyApp
[Install]
WantedBy=multi-user.target
Here is the error from /var/log/syslog
Oct 14 13:00:55 devu18 systemd[1]: Started myapp.
Oct 14 13:00:55 devu18 systemd[3203]: myapp.service: Changing to the requested working directory failed: No such file or directory
Oct 14 13:00:55 devu18 systemd[3203]: myapp.service: Failed at step CHDIR spawning /usr/bin/npm: No such file or directory
Oct 14 13:00:55 devu18 systemd[1]: myapp.service: Main process exited, code=exited, status=200/CHDIR
Oct 14 13:00:55 devu18 systemd[1]: myapp.service: Failed with result 'exit-code'.
I for the life of me can't figure out why it's complaining of cannot find the file. npm start from the same working directory works just fine, no problems. Am I missing some permissions or +x or something somewhere?
This error might happen due to the fact that the executable was run without environment.
Here is a quick fix recipe
You can fix that by creating a Bash Script to do everything you need.
Now change it's mode to be executable
Now we can create our service (located for instance in
/etc/systemd/system/my-project.service
)And now let's run it and enable it
Troubleshooting (Ubuntu 18.04 LTS)
This happened to me lately that for some reason systemctl couldn't effectively source
.bashrc
leaving me with an errornpm error does not exists
This is a workaround that helped me out.Change the script.sh file's content into the following
Now your npm service should hopefully work.
This solution was tested on: