AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1011413
Accepted
Nissy A.W.
Nissy A.W.
Asked: 2020-04-09 01:59:02 +0800 CST2020-04-09 01:59:02 +0800 CST 2020-04-09 01:59:02 +0800 CST

NodeJS反应系统服务不工作

  • 772

我正在尝试为一个简单的反应应用程序设置一个 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.

如果我尝试通过 systemd 启动应用程序,它会失败但没有说明原因。我使用相同的用户从相同的路径启动它,并尝试了 ExecStart 可以想象的所有组合:

ExecStart=npm start
ExecStart=/usr/bin/npm start
ExecStart=/usr/bin/node /home/myuser/test/node_modules/react-scripts/scripts/start.js

它们都产生相同的结果,即它“开始”正常,并且在它失败后不久,在 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...

几秒钟后……

$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)

我在 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/

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

操作系统是Centos7。所有测试都是使用相同的用户完成的,没有 sudo,手动和通过 systemd 使用相同的命令。手动启动有效,systemd 无效。

最后,提一下,我通过 systemd 拥有 Express-NodeJS 应用程序,并且它使用相同的 systemd 配置正确启动。它只对 React 失败。

任何帮助将不胜感激!

谢谢

尼西

systemd node.js npm
  • 3 3 个回答
  • 3195 Views

3 个回答

  • Voted
  1. Best Answer
    lzb
    2020-04-09T05:04:17+08:002020-04-09T05:04:17+08:00

    升级后我遇到了同样的问题,我的 systemd 服务每次启动时都会以错误代码 0 退出。

    这是由于 2 月对 react-scripts start.js 的更新

    Kolega 的回答确实是解决此问题的最佳方法:

    将 CI(持续集成)环境变量设置为 true。这可以在服务文件中完成:

    Environment=CI=true
    

    这是一个示例 .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
    

    如果上述方法对您有用,请投票赞成 Kolega 的答案。

    我的原始解决方案如下,这需要更改以响应分发文件:

    为了解决这个问题,我注释掉了在标准输入结束时使 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();
        }
    

    标准输入结束时 start.js 更改为退出记录在https://github.com/facebook/create-react-app/pull/7203/files/108bafe5d5b85c9544dd05b5ed42b4e90c66b2ca

    • 4
  2. Steve Klein
    2020-08-23T09:51:46+08:002020-08-23T09:51:46+08:00

    我通过将以下内容添加到我的 .service 文件的 [Service] 部分解决了这个问题:

    标准输入=tty-force

    • 3
  3. Ondřej Kolín
    2021-01-08T01:21:23+08:002021-01-08T01:21:23+08:00

    如果你想拥有不退出的服务,npm start你必须定义一个CI变量来防止在阅读后退出EOF。该解决方案也可以在用户会话中运行。我的服务文件中的重要部分是Environment=CI=True.

    [Unit]
    Description=Project
    
    [Service]
    ExecStart=/usr/bin/npm run start --prefix /home/ondrej/project
    Environment=CI=true
    
    [Install]
    WantedBy=default.target
    
    • 1

相关问题

  • SSH 会话在关机/重启时挂起

  • 我可以使用 systemd 重新启动依赖服务吗?

  • 让我的旧初始化脚本在 systemd 中工作的最简单方法是什么?

  • 在 CentOS 7 上为 systemd 启动的进程增加 nproc

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve