Nis*_*.W. 5 node.js systemd npm
我正在尝试为一个简单的 React 应用程序设置一个 systemd 服务。此应用程序托管在 /home/myuser/test 中。npm 和 node 都在 PATH 中并且硬链接到 /usr/bin。所有文件都具有 myuser:myuser 用户和组的权限。如果我手动启动它,npm start它会正确启动并从http://localhost:3000
Compiled successfully!
You can now view test in the browser.
Local: http://localhost:3000
On Your Network: http://myipaddress:3000
Note that the development build is not optimized.
To create a production build, use npm run build.
Run Code Online (Sandbox Code Playgroud)
如果我尝试通过 systemd 启动应用程序,它会失败但没有说明原因。我从相同的路径、相同的用户启动它,并尝试了 ExecStart 可以想象的所有组合:
ExecStart=npm start
ExecStart=/usr/bin/npm start
ExecStart=/usr/bin/node /home/myuser/test/node_modules/react-scripts/scripts/start.js
Run Code Online (Sandbox Code Playgroud)
它们都产生相同的结果,即它“开始”正常,但很快就失败了,在 journalctl 中显然没有理由:
$sudo systemctl status node-client
? node-client.service - Node-React Frontend Server
Loaded: loaded (/etc/systemd/system/node-client.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2020-04-08 09:46:10 UTC; 679ms ago
Main PID: 18165 (node)
CGroup: /system.slice/node-client.service
??18165 /usr/bin/node /home/myuser/test/node_modules/react-scripts/scripts/start.js
Apr 08 09:46:10 hostname systemd[1]: Started Node-React Frontend Server.
Apr 08 09:46:10 hostname systemd[1]: Starting Node-React Frontend Server...
Run Code Online (Sandbox Code Playgroud)
几秒钟后……
$sudo systemctl status node-client
? node-client.service - Node-React Frontend Server
Loaded: loaded (/etc/systemd/system/node-client.service; disabled; vendor preset: disabled)
Active: activating (auto-restart) since Wed 2020-04-08 09:46:00 UTC; 3s ago
Process: 18142 ExecStart=/usr/bin/node /home/ec2-user/test/node_modules/react-scripts/scripts/start.js (code=exited, status=0/SUCCESS)
Main PID: 18142 (code=exited, status=0/SUCCESS)
Run Code Online (Sandbox Code Playgroud)
我在 journalctl 上不断收到以下信息
-- Logs begin at Tue 2020-03-17 15:04:59 UTC, end at Wed 2020-04-08 09:48:23 UTC. --
Apr 08 09:48:22 hostname systemd[1]: Starting Node-React Frontend Server...
Apr 08 09:48:22 hostname systemd[1]: Started Node-React Frontend Server.
Apr 08 09:48:22 hostname systemd[1]: node-client.service holdoff time over, scheduling restart.
Apr 08 09:48:12 hostname nodeclient[18390]: Starting the development server...
Apr 08 09:48:12 hostname nodeclient[18390]: ? ?wds?: 404s will fallback to /
Apr 08 09:48:12 hostname nodeclient[18390]: ? ?wds?: Content not from webpack is served from /home/myuser/test/public
Apr 08 09:48:12 hostname nodeclient[18390]: ? ?wds?: webpack output is served from
Apr 08 09:48:12 hostname nodeclient[18390]: ? ?wds?: Project is running at http://myipaddress/
Apr 08 09:48:10 hostname systemd[1]: Starting Node-React Frontend Server...
Apr 08 09:48:10 hostname systemd[1]: Started Node-React Frontend Server.
Apr 08 09:48:10 hostname systemd[1]: node-client.service holdoff time over, scheduling restart.
Apr 08 09:48:00 hostname nodeclient[18368]: Starting the development server...
Apr 08 09:48:00 hostname nodeclient[18368]: ? ?wds?: 404s will fallback to /
Apr 08 09:48:00 hostname nodeclient[18368]: ? ?wds?: Content not from webpack is served from /home/myuser/test/public
Apr 08 09:48:00 hostname nodeclient[18368]: ? ?wds?: webpack output is served from
Apr 08 09:48:00 hosntame nodeclient[18368]: ? ?wds?: Project is running at http://myipaddress/
Run Code Online (Sandbox Code Playgroud)
systemd 服务文件随着时间的推移发生了变化,但无论 ExecStart 和其他变化如何,错误都是一样的,这是截至目前的内容:
[Unit]
Description=Node-React Frontend Server
After=syslog.target network.target
[Service]
ExecStart=/usr/bin/node node_modules/react-scripts/scripts/start.js
Restart=always
RestartSec=10s
TimeoutSec=900
User=myuser
Group=myuser
Environment=PATH=/usr/bin:/usr/local/bin
WorkingDirectory=/home/myuser/test/
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodeclient
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
操作系统是 Centos7。所有测试都是使用相同的用户完成的,没有 sudo,手动和通过 systemd 使用相同的命令。手动启动有效,systemd 无效。
最后,值得一提的是,我通过 systemd 拥有 Express-NodeJS 应用程序,并且它使用相同的 systemd 配置正确启动。它只对 React 失败。
任何帮助将不胜感激!
谢谢
尼西
升级后我遇到了同样的问题,我的 systemd 服务每次启动时都会退出并显示错误代码 0。
这是由于二月份对react-scripts start.js 的更新造成的
Kolega 的答案确实是解决这个问题的最佳方法:
将 CI(持续集成)环境变量设置为 true。这可以在服务文件中通过以下方式完成:
Environment=CI=true
Run Code Online (Sandbox Code Playgroud)
这是一个示例 .service 文件:
# Sample - My Sample Service file
[Unit]
Description=Sample - Sample Service to start a React server
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/npm start --prefix /home/pi/sample
Environment=CI=true
WorkingDirectory=/home/pi/sample
StandardOutput=inherit
StandardError=inherit
Restart=no
User=pi
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
如果以上内容对您有用,请为 Kolega 的答案投票。
我原来的解决方案如下,需要更改反应分发文件:
为了解决这个问题,我注释掉了 stdin 结束时使 start.js 退出的更改。由于服务没有标准输入,因此它在应用程序启动之前就存在。这是在 node-modules/react-scripts/scripts/start.js 中
if (isInteractive || process.env.CI !== 'true') {
// Gracefully exit when stdin ends
// process.stdin.on('end', function() {
// devServer.close();
// process.exit();
// });
process.stdin.resume();
}
Run Code Online (Sandbox Code Playgroud)
当标准输入结束时 start.js 更改为退出记录在https://github.com/facebook/create-react-app/pull/7203/files/108bafe5d5b85c9544dd05b5ed42b4e90c66b2ca
| 归档时间: |
|
| 查看次数: |
1870 次 |
| 最近记录: |