티스토리 뷰

React.js 를 사용해 개발하다가 완성이 되면 npm start를 이용해 개발 서버(디폴트 3000번)을 사용하는 것이 아닌

npm(yarn) build 를 이용해서 나온 아웃풋을 보통 호스팅 하게 됩니다.

하지만 가끔 어쩔 수 없는 이유로 npm start를 이용해 운영을 하게 될 수 도 있는데요.

이런 상황에서 Linux Systemd 서비스로 등록해서 부팅시 자동으로 개발 서버가 실행되게 세팅 해야하는 환경에서 제가 겪은 이슈입니다.

우선 create-react-app 으로 생성한 코드를 기준으로 하겠습니다.

sudo vi /etc/systemd/system/reactrun.service

를 통해 react 개발 서버를 구동하기 위한 service를 만듭니다.

systemd 서비스 등록을 위한 .service 템플릿은 추후에 자세히 포스팅 하겠습니다.

 

[Unit]
Description=React App run

[Service]
Type=simple
User=kimsehwan
ExecStart=/bin/usr/npm start
TimeoutStartSec=0
RemainAfterExit=yes

WorkingDirectory=/home/kimsehwan/frontcode/templates

[Install]
WantedBy=default.target

이렇게 작성하고

sudo systemctl daemon-reload
sudo systemctl enable reactrun
sudo systemctl start reactrun

이렇게 작성하면 실행이 되어야 하지만..

 $Sudo systemctl status reactrun 
 ● reactrun.service - Node-React Frontend Server
   Loaded: loaded (/etc/systemd/system/reactrun.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-09-06 09:48:22 UTC; 679ms ago
 Main PID: 18121 (node)
   CGroup: /system.slice/reactrun.service
           └─18165 /usr/bin/node /home/kimsehwan/frontcode/templates

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...

 

이렇게 콘솔 출력이 나오다가

 

Sep 06 09:48:22 hostname systemd[1]: Starting Node-React Frontend Server...
Sep 06 09:48:22 hostname systemd[1]: Started Node-React Frontend Server.
Sep 06 09:48:22 hostname systemd[1]: reactrun.service holdoff time over, scheduling restart.
Sep 06 09:48:12 hostname nodeclient[18390]: Starting the development server...
Sep 06 09:48:12 hostname nodeclient[18390]: ℹ 「wds」: 404s will fallback to /
Sep 06 09:48:12 hostname nodeclient[18390]: ℹ 「wds」: Content not from webpack is served from /home/kimsehwan/frontcode/templates
Sep 06 09:48:12 hostname nodeclient[18390]: ℹ 「wds」: webpack output is served from
Sep 06 09:48:12 hostname nodeclient[18390]: ℹ 「wds」: Project is running at http://localhost/
Sep 06 09:48:10 hostname systemd[1]: Starting Node-React Frontend Server...
Sep 06 09:48:10 hostname systemd[1]: Started Node-React Frontend Server.
Sep 06 09:48:10 hostname systemd[1]: node-client.service holdoff time over, scheduling restart.
Sep 06 09:48:00 hostname nodeclient[18368]: Starting the development server...
Sep 06 09:48:00 hostname nodeclient[18368]: ℹ 「wds」: 404s will fallback to /
Sep 06 09:48:00 hostname nodeclient[18368]: ℹ 「wds」: Content not from webpack is served from /home/kimsehwan/frontcode/templates
Sep 06 09:48:00 hostname nodeclient[18368]: ℹ 「wds」: webpack output is served from
Sep 06 09:48:00 hosntame nodeclient[18368]: ℹ 「wds」: Project is running at http://localhost/

이렇게 출력이 나옵니다. 갑자기 개발 서버가 죽어버리는 것이죠 ㅠㅠ

이 때 해결 방법은 !

$ vi node_modules/react-scripts/scripts/start.js 로 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();
    }

 

이 부분을 주석처리 하면 됩니다.

사용자와 인터랙트 할 수 없는 환경에서는 개발 서버가 죽어버리게 업데이트를 한 것 같습니다.

해당 업데이트 : https://github.com/facebook/create-react-app/pull/7203/files/108bafe5d5b85c9544dd05b5ed42b4e90c66b2ca

도움이 되었으면 좋겠습니다.