Setup/aws

[AWS 무료 서버구축-16/18] SpringBoot 백그라운드에서 실행하기

goodsaem 2021. 7. 25. 16:50

SpringBoot 백그라운드에서 실행하기

아래 명령어로 샘플 프로젝트를 실행하면 foreground 로 명령어가 실행됩니다. ctrl + c 키를 눌러서 종료하면 프로세스가 종료됩니다. 명령어를 실행하고 나서 로그아웃을 하더라도 명령어가 계속 실행된 상태로 있을려면 background 로 명령어가 실행되어야 합니다. 

java -jar hello-0.0.1-SNAPSHOT.war

background로 실행하는 프로세스를 만들기 위해 아래와 같이 start.sh 파일을 생성합니다.

ubuntu@goodsaem:~$ vi start.sh

nohup을 사용하여 출력을 nohup.out으로 보내고 명령어를 실행한다음 & 같이 입력하여 백그라운드에서 실행 되게 만듭니다.

nohup java -jar hello*.war &

그리고 나서 start.sh 실행 권한을 부여 합니다.

ubuntu@goodsaem:~$ chmod +x start.sh

stop 스크립트를 작성합니다.

ubuntu@goodsaem:~$ vi stop.sh

아래와 같이 입력합니다. 프로세스를 찾아서 kill 하는 명령어 입니다.

#!/bin/sh

PID=`ps -ef | grep java | grep war | awk '{print $2}'`
if [ -n "$PID" ]
then
  echo "=====spring is running at" $PID "Shutdown spring now"
  kill -9 $PID
else
  echo "=====spring isn't running====="
fi

저장하고 종료합니다. log 출력 스크립트를 작성합니다.

ubuntu@goodsaem:~$ vi log.sh

아래와 같이 입력합니다

tail -f nohup.out

stop.sh 와 log.sh 각각 실행권한을 부여합니다.

ubuntu@goodsaem:~$ chmod +X start.sh
ubuntu@goodsaem:~$ chmod +X log.sh

서비스를 시작합니다.

ubuntu@goodsaem:~$ ./start.sh;./log.sh

아래와 같은 로그 확인이 가능합니다.

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

2021-03-27 02:23:27.115  INFO 3532 --- [           main] g.github.io.hello.HelloApplication       : Starting HelloApplication using Java 1.8.0_282 on goodsaem with PID 3532 (/home/ubuntu/hello-0.0.1-SNAPSHOT.war started by ubuntu in /home/ubuntu)
2021-03-27 02:23:27.124  INFO 3532 --- [           main] g.github.io.hello.HelloApplication       : No active profile set, falling back to default profiles: default
2021-03-27 02:23:29.570  INFO 3532 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9090 (http)
2021-03-27 02:23:29.600  INFO 3532 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-03-27 02:23:29.606  INFO 3532 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-03-27 02:23:30.416  INFO 3532 --- [           main] o.a.c.c.C.[.[localhost].[/spring]        : Initializing Spring embedded WebApplicationContext
2021-03-27 02:23:30.416  INFO 3532 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3156 ms
2021-03-27 02:23:31.421  INFO 3532 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-27 02:23:31.886  INFO 3532 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9090 (http) with context path '/spring'
2021-03-27 02:23:31.908  INFO 3532 --- [           main] g.github.io.hello.HelloApplication       : Started HelloApplication in 6.02 seconds (JVM running for 6.993)

이제 ctrl + c 를 해도 프로세스가 종료되지 않습니다.

 

 

서비스 종료

아래 명령어로 서비스를 종료 합니다.

ubuntu@goodsaem:~$ ./stop.sh;./log.sh
=====spring is running at 3532 Shutdown spring now

다시 서비스를 시작합니다.

ubuntu@goodsaem:~$ ./start.sh;./log.sh

 

테스트

이제 실제로 https 로 접속을 시도해 보겠습니다. http://goodsaem.ml/spring/goodsaem/string (opens new window) https 프로토콜을 이용하여 설정한 도메인으로 접속하니 아래 그림과 같이 정상적으로 화면이 나왔습니다.