처음에는 EC2의 사용자 데이터에 스크립트만 입력을 해서 테스트를 해봤지만 스크립트는 실행이 되지 않았다.
EC2 인스턴스 실행 시 스크립트를 실행하여 애플리케이션 프로세스가 안 떠 있으면 애플리케이션을 실행하고 싶었다.
그 이유가 무엇인지 찾아보니 사용자 데이터는 최초 실행 시에만 실행된다고 하는것 같더라.
그러니까 중지 후 다시 시작을 해도 스크립트는 실행이 안된다고 한다.
그래서 해결 방법을 AWS 문서에서 찾았다.
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
check=`ps -ef | grep 'dev-0.0.1-SNAPSHOT.jar' | wc | awk '{print $1}'`
if [ $check -gt 1 ]
then
echo "RUNNING"
else
cd /home/ec2-user/test-application
nohup java -jar ./dev-0.0.1-SNAPSHOT.jar 1> /dev/null 2>&1 &
fi
--//--
참조 :
https://aws.amazon.com/ko/premiumsupport/knowledge-center/execute-user-data-ec2/
[AWS/CloudFormation/Scheduler] EC2 인스턴스 CloudFormation을 사용해서 스케줄러 적용하기 (0) | 2022.05.20 |
---|---|
[AWS/Postgresql] Postgresql 테이블 트리거(Trigger)로 람다(Lambda) 호출하기 (0) | 2022.05.20 |
댓글 영역