Systemd .service 文件不起作用

doo*_*ers 8 nodejs systemd

我正在尝试使用 systemd 在系统启动时自动运行以下命令。

/usr/bin/node /var/www/html/rest-api/dist/index.js

我已经通过手动运行来验证该命令是否有效,但是当我尝试使用rest.service文件启动它时,我遇到了错误。

休息服务:

[Unit]
Description=REST API
After=network.target

[Service]
ExecStart=/usr/bin/node /var/www/html/rest-api/dist/index.js
Restart=always
User=nobody
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/rest-api/dist

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

journalctl -u rest-api 输出:

其中rest.service用户和组 = 无人:

Oct 06 02:10:28 ip-172-31-26-208 systemd[7172]: rest-api.service: Failed at step GROUP spawning /usr/bin/node: No such process
Oct 06 02:10:28 ip-172-31-26-208 systemd[1]: rest-api.service: Main process exited, code=exited, status=216/GROUP
Oct 06 02:10:28 ip-172-31-26-208 systemd[1]: rest-api.service: Unit entered failed state.
Oct 06 02:10:28 ip-172-31-26-208 systemd[1]: rest-api.service: Failed with result 'exit-code'.
Oct 06 02:10:29 ip-172-31-26-208 systemd[1]: rest-api.service: Service hold-off time over, scheduling restart.
Oct 06 02:10:29 ip-172-31-26-208 systemd[1]: Stopped REST API. 
Run Code Online (Sandbox Code Playgroud)

其中rest.service用户和组 = root:

Oct 06 02:20:11 ip-172-31-26-208 systemd[1]: Started REST API.
Oct 06 02:20:11 ip-172-31-26-208 systemd[1]: rest-api.service: Main process exited, code=exited, status=200/CHDIR
Oct 06 02:20:11 ip-172-31-26-208 systemd[1]: rest-api.service: Unit entered  failed state.
Oct 06 02:20:11 ip-172-31-26-208 systemd[1]: rest-api.service: Failed with  result 'exit-code'.
Oct 06 02:20:12 ip-172-31-26-208 systemd[1]: rest-api.service: Service hold-off > time over, scheduling restart.
Oct 06 02:20:12 ip-172-31-26-208 systemd[1]: Stopped REST API.
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决?

小智 7

code=exited, status=200/CHDIR 是您的关键错误信息。

它表示/var/www/rest-api/dist在您的服务尝试运行时不存在或不可访问。

如果它是通过网络安装的,After=network.target并不一定意味着已经安装了任何给定的网络安装。甚至可能尚未安装启动缓慢的本地驱动器。要查看您是否遇到竞争条件,请尝试添加延迟或可能使用RequiresMountsFor=, 甚至ConditionPathExists=.

来源:https : //www.freedesktop.org/software/systemd/man/systemd.unit.html

  • 虽然我想知道为什么我不能以无人身份而不是 root 身份运行它。对此有何想法? (3认同)