最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何将Node.js应用程序作为systemd服务运行?

IT培训 admin 7浏览 0评论

如何将Node.js应用程序作为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: 
-- 
-- 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: 
-- 
-- 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: 
-- 
-- Unit my-app.service has finished shutting down.

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

回答如下:

通过运行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

如何将Node.js应用程序作为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: 
-- 
-- 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: 
-- 
-- 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: 
-- 
-- Unit my-app.service has finished shutting down.

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

回答如下:

通过运行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
发布评论

评论列表 (0)

  1. 暂无评论