Linux

CentOS7에 Springboot war파일 서비스 등록하기

딸기케잌🍓 2020. 8. 26. 00:05

[CentOS7에 Springboot War 파일 서비스 등록]

 

 

centOS7에 서비스 등록하는 방법을 엄청난 삽질끝에 해냈당,,,TㅅT 여러 영문사이트를 오가며 이거저거 다 해봤지만 안되다가...해결한 일기를 간단하게! 적어놓는다!!

 

 

서비스 등록하는 방법은 다음과 같다.

 

 

1. /etc/systemd/system 경로에 myApp.service파일을 만든다.

vi /etc/systemd/system myApp.service

2. myApp.service에 다음과 같이 입력해준다.

[Unit]
Description=myApp
After=syslog.target

[Service]
User=root
ExecStart=[myApp경로]/myApp.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

 

3. myApp.jar파일을 실행할 수 있게 바꾸어준다.

chmod +x myApp.jar

 

4.다음과 같은 명령어를 실행하여 앱을 기동한다.

systemctl daemon-reload
systemctl enable myApp.service
systemctl start myApp.service
systemctl status myApp.service

 

 

1,2,3,4만 보면 간단해서 뙇!하고 될것같은데.. 기대와는 다르게 역시 쉽지않쥐,,

 

 

내가 서비스에 등록하려는 app은 Coverage.war파일인데

다른 포스팅들을 보면 다들 .jar 파일들을 등록하는 예제들이였고 이상하게 기동이 되지 않았다,,

 

 

 

(code=exited, status=203/EXEC) 에러

 

이 에러를 찾아보니 myApp.service 파일에서 ExecStart에 /bin/bash를 붙여보라고 되어있었다.

실행 명령어는 반드시 절대 경로 또는 변수(${STRINGS}와 같이)로 시작해야 한다고 한다!

myApp.service에서 다음과 같이 수정했다.

ExecStart=[myApp경로]/myApp.jar  --> ExecStart=/bin/bash [myApp경로]/myApp.jar

 

그랬더니 또 다른 에러가...

(code=exited, status=126) 에러

 

왜 cannot execute binary file이냐고!!!!!!

 

그러다가 .war파일을 다른 방식으로 실행해 봐야겠다고 짱구를 굴렸고,,

다음과 같이 수정하고 해당경로에 startup.sh를 만들었다.

ExecStart=/bin/bash [myApp경로]/myApp.jar --> ExecStart=/bin/bash [startup.sh경로]/startup.sh

 

 

startup.sh는 다음과 같다.

#! /bin/bash

echo "Coverage Started!!"
java -jar -Dspring.profiles.active=dev /Coverage.war

exit 0

 

 

그리고 다시

systemctl daemon-reload
systemctl enable myApp.service
systemctl start myApp.service
systemctl status myApp.service

 

위 명령어들을 실행 후 status를 보니

감격스러운 active(running) 상태가 되었따!!

 

 

shutdown시켜볼까!?

systemctl stop Coverage

stop도 문제 없고 status도 inactive(dead)로 잘나온다

 

 

 

 

 

 

최종 Coverage.service는 다음과 같다.

[Unit]
Description=Coverage
After=syslog.target

[Service]
User=root
ExecStart=/bin/bash /startup.sh
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

 

이게 뭐라고 이렇게 행복햄 ㅜㅜㅜㅠㅠ

 

 

 

20.08.27 추가

Error:Unable to access jarfile 

다른 서버에서 서비스 등록 중 위와 같은 에러가 났다.

Error: Unable to access jarfile

찾아보니 startup.sh 스크립트에서 war파일 경로를 잘못 적어주어서 그랬다!!

 

#! /bin/bash

echo "Coverage Started!!"
java -jar -Dspring.profiles.active=dev /Coverage.war --> /home/truecover/smartuCoverage/Coverage.war

exit 0


/Coverage.war 이 부분을 절대경로를 이용하여 수정했다.

서비스 등록 성공!!

'Linux' 카테고리의 다른 글

sudo, su, su - 차이점  (0) 2020.07.29