大网数据广告活动
服务器运维问答

How to configure Systemd service unit to start Node app with "npm start" instead of "app.js"

· 52CE 我爱测

Environment: CentOS 8, Node.js, Digital Ocean Droplet My Systemd setup starts a node app with the following command. It works as expected. $ sudo systemctl start myapp File 1: /etc/systemd/system/myapp.service [Unit] Description …

问题原文

原帖链接 · myNewAccount · CC BY-SA 4.0

Environment: CentOS 8, Node.js, Digital Ocean Droplet

My Systemd setup starts a node app with the following command. It works as expected.

$ sudo systemctl start myapp

File 1: /etc/systemd/system/myapp.service

[Unit]
Description = My App 
After = network.target

[Service]
ExecStart = /root/start-myapp.sh
Restart=on-failure

[Install]
WantedBy = multi-user.target

File 2: /root/start-myapp.sh

#!/bin/sh
/usr/bin/node /srv/myapp/app.js

However when I'm not using Systemd I normally start my app with Node Package Manager using the command, npm start. I'd rather have the Systemd unit also start the app with npm start.

Unfortunately it threw an error when I changed app.js to npm start in /root/start-myapp.sh,

/usr/bin/node /srv/myapp/npm start

I made another attempt by switching the path from node to npm but it produced the same error.

/usr/bin/npm /srv/myapp/npm start

Question: How do I get my Systemd unit file to start my app with the command npm start?

社区高分解答

原帖链接 · filipe · CC BY-SA 4.0

I'm running a similar setup on Debian, and my service looks like this:

...

[Service]
Environment=NODE_PORT=3000
Type=simple
User=www-data
Restart=on-failure
WorkingDirectory=/var/www/<your-code-directory>
ExecStart=npm start

...

Hope this helps!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-05-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。