我如何 运行 一个 Node.js 应用作为 systemd 服务?

How do I run a Node.js app as a systemd service?

所以我有一个 node.js 应用程序,我正在尝试部署 运行 作为 systemd 服务。

这是 .service 文件:

[Unit]
Description=My app

[Service]
ExecStart=/usr/local/bin/node /var/www/html/schema.js
Restart=always
User=root
Group=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/html

[Install]
WantedBy=multi-user.target

不幸的是,它不起作用....我一直收到错误消息:

Feb 12 09:56:49 myswerth systemd[1]: Started my-app app.
-- Subject: Unit my-app.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit my-app.service has finished starting up.
-- 
-- The start-up result is done.
Feb 12 09:56:49 my-server systemd[23765]: Failed at step GROUP spawning /usr/local/bin/node: No such process
-- Subject: Process /usr/local/bin/node could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- The process /usr/local/bin/node could not be executed and failed.
-- 
-- The error number returned by this process is 3.
Feb 12 09:56:49 myswerth systemd[1]: my-app.service: main process exited, code=exited, status=216/GROUP
Feb 12 09:56:49 myswerth systemd[1]: Unit my-app.service entered failed state.
Feb 12 09:56:49 myswerth systemd[1]: my-app.service failed.
Feb 12 09:56:49 myswerth systemd[1]: my-app.service holdoff time over, scheduling restart.
Feb 12 09:56:49 myswerth systemd[1]: Stopped My app app.
-- Subject: Unit my-app.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit my-app.service has finished shutting down.

我不太确定为什么会出现此错误。我已经尝试手动访问该文件并可以确认它位于 /usr/local/bin/node 中,而且我可以通过 运行 打开节点(我可以吗?)所以我不知道为什么它不能执行?我还检查了执行权限。所有用户组都可以执行所以你....我在这里不知所措...

通过 运行 which node

确保 node 在路径中

尝试以下操作:

在您的 schema.js 文件中,在顶部添加以下行

#!/usr/bin/env node

这将消除在 ExecStart

中指定节点的需要

还要确保

chmod +x /var/www/html/schema.js

值得考虑添加到服务定义的其他选项:

PIDFile=/tmp/your-app-name.pid
KillSignal=SIGQUIT
WorkingDirectory=/var/www/html/

尝试修复使用:

Group=root