如何将Node.js应用作为系统服务运行?

时间:2019-02-12 11:13:44

标签: node.js centos7 systemd

因此,我有一个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中,而且可以通过运行它来打开节点(可以吗?),所以我不知道为什么它不能执行?我还检查了执行权限。所有用户组都可以执行,所以....我在这里很茫然...

1 个答案:

答案 0 :(得分:0)

通过运行node

确保which 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