하나씩 차근차근

앞에서 EC2 서버에 스프링부트 예제 프로젝트를 설치하고 실행을 했습니다.

다만, MobaXterm 프로그램을 종료하면 EC2 서버와의 세션이 끊어지게 되어 프로젝트도 종료됩니다.

이때 세션이 끊어져도 프로젝트를 계속 실행시킬 수 있도록 해보겠습니다.

 

nohup

리눅스에서 터미널의 세션 연결이 끊어지더라도 프로그램을 계속 실행시켜주는 명령어

ubuntu@ip-172-31-12-184:~/aws-v1/build/libs$ nohup java -jar v1-0.0.1-SNAPSHOT.jar
nohup: ignoring input and appending output to 'nohup.out'

위와 같이 nohup 명령어를 사용해 예제 프로젝트를 실행합니다.

터미널을 종료시켜도 브라우저를 통해 페이지에 접속할 수 있습니다.

ubuntu@ip-172-31-12-184:~/aws-v1/build/libs$ nohup java -jar v1-0.0.1-SNAPSHOT.jar &
[1] 12236
ubuntu@ip-172-31-12-184:~/aws-v1/build/libs$ nohup: ignoring input and appending output to 'nohup.out'

nohup 명령어는 & (백그라운드로 실행) 과 함께 쓰입니다.

위와 같이 & 를 붙여서 실행하면 백그라운드에서 프로젝트를 실행시키게 됩니다.

nohup 은 nohup.out 파일을 생성해서 로그를 남깁니다.

ubuntu@ip-172-31-12-184:~/aws-v1/build/libs$ ls
nohup.out  v1-0.0.1-SNAPSHOT.jar

cat nohup.out 을 통해 확인해보면 로그가 기록된것을 볼 수 있습니다.

ubuntu@ip-172-31-12-184:~/aws-v1/build/libs$ cat nohup.out

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.3)

2023-01-01 23:36:45.512  INFO 11973 --- [           main] com.metacoding.v1.V1Application          : Starting V1Application using Java 11.0.17 on ip-172-31-12-184 with PID 11973 (/home/ubuntu/aws-v1/build/libs/v1-0.0.1-SNAPSHOT.jar started by ubuntu in /home/ubuntu/aws-v1/build/libs)
2023-01-01 23:36:45.520  INFO 11973 --- [           main] com.metacoding.v1.V1Application          : No active profile set, falling back to default profiles: default
2023-01-01 23:36:47.785  INFO 11973 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-01-01 23:36:47.808  INFO 11973 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-01-01 23:36:47.812  INFO 11973 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2023-01-01 23:36:47.952  INFO 11973 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-01-01 23:36:47.952  INFO 11973 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2308 ms
2023-01-01 23:36:49.290  INFO 11973 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-01-01 23:36:49.319  INFO 11973 --- [           main] com.metacoding.v1.V1Application          : Started V1Application in 4.937 seconds (JVM running for 5.976)
2023-01-01 23:39:02.744  INFO 11973 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-01-01 23:39:02.748  INFO 11973 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-01-01 23:39:02.753  INFO 11973 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms
2023-01-01 23:39:02.826  INFO 11973 --- [nio-8080-exec-1] com.metacoding.v1.web.HelloController    : /aws/v1 이 호출되었어요. info 로그 #####################################

 

netstat 명령어를 통해 현재 포트 상태를 확인해보겠습니다.

먼저 sudo apt install net-tools 를 입력해서 netstat 를 설치합니다.

ubuntu@ip-172-31-12-184:~/aws-v1/build/libs$ sudo apt install net-tools
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  net-tools
0 upgraded, 1 newly installed, 0 to remove and 18 not upgraded.
Need to get 196 kB of archives.
After this operation, 864 kB of additional disk space will be used.
Get:1 http://ap-northeast-2.ec2.archive.ubuntu.com/ubuntu focal/main amd64 net-t                                                                                                                              ools amd64 1.60+git20180626.aebd88e-1ubuntu1 [196 kB]
Fetched 196 kB in 0s (925 kB/s)
Selecting previously unselected package net-tools.
(Reading database ... 63610 files and directories currently installed.)
Preparing to unpack .../net-tools_1.60+git20180626.aebd88e-1ubuntu1_amd64.deb ..                                                                                                                              .
Unpacking net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...
Setting up net-tools (1.60+git20180626.aebd88e-1ubuntu1) ...
Processing triggers for man-db (2.9.1-1) ...

nohup 를 통해 실행시킨 예제 프로젝트를 확인해보겠습니다.

ubuntu@ip-172-31-12-184:~/aws-v1/build/libs$ netstat -nlpt
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      -
tcp6       0      0 ::1:6010                :::*                    LISTEN      -
tcp6       0      0 :::8080                 :::*                    LISTEN      12236/java
tcp6       0      0 127.0.0.1:43101         :::*                    LISTEN      11000/java
tcp6       0      0 :::22                   :::*                    LISTEN      -

netstat -nlpt 를 입력하면 8080 포트가 프로젝트를 통해 사용하고 있는것을 볼 수 있습니다.

 

이 포스트는 유튜브에 올라온 메타코딩님의 AWS 강의를 보고 정리한 내용입니다.
https://www.youtube.com/@metacoding

 

'AWS' 카테고리의 다른 글

AWS - 프로젝트 배포하기  (0) 2023.01.01
AWS - EC2 서버와 방화벽  (0) 2022.12.30
AWS - EC2 서버 접속하기  (0) 2022.12.30
AWS - EC2 서버 임대하기  (0) 2022.12.30
profile

하나씩 차근차근

@jeehwan_lee

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!