✅ 1. nohup 사용 (간단한 방법)
터미널을 종료해도 애플리케이션이 백그라운드에서 실행되도록 합니다.
nohup java -jar your-app.jar > output.log 2>&1 &
- nohup : 터미널이 종료되어도 프로세스가 계속 실행됨
- > : 로그를 output.log에 저장
- 2>&1 : 표준 오류 로그도 동일한 파일에 저장
- & : 백그라운드에서 실행
실행 중인 프로세스를 확인하려면:
ps aux | grep java
✅ 2. tmux 또는 screen 사용
터미널 세션을 유지하면서 실행하는 방법입니다.
(1) tmux 사용
tmux new -s myapp java -jar your-app.jar
- 터미널을 종료해도 계속 실행됨
- 다시 접속하려면: tmux attach -t myapp
(2) screen 사용
screen -S myapp java -jar your-app.jar
- 터미널을 종료해도 유지됨
- 다시 접속하려면: screen -r myapp
✅ 3. systemd 서비스로 등록 (이게 좋음)
서버가 재부팅되어도 자동으로 실행되도록 설정합니다.
1️⃣ 서비스 파일 생성
sudo nano /etc/systemd/system/myapp.service
2️⃣ 다음 내용 추가
[Unit] Description=My Kotlin Spring Boot App After=network.target
[Service] User=ec2-user WorkingDirectory=/home/ec2-user ExecStart=/usr/bin/java -jar /home/ec2-user/your-app.jar SuccessExitStatus=143 Restart=always RestartSec=5 StandardOutput=journal StandardError=journal
[Install] WantedBy=multi-user.target
3️⃣ 서비스 등록 및 실행
sudo systemctl daemon-reload sudo systemctl enable myapp sudo systemctl start myapp
4️⃣ 상태 확인
sudo systemctl status myapp
5️⃣ 중지 및 다시 시작
sudo systemctl stop myapp sudo systemctl restart myapp
'TIL' 카테고리의 다른 글
spring servlet 정리. (1) | 2025.03.17 |
---|---|
DB 비관적 Lock vs 낙관적 Lock (0) | 2025.03.17 |
GitHub action SSH key 발급 (0) | 2025.03.15 |
NginX 설정 https 요청시 주의점 (0) | 2025.03.14 |
Nginx 프록시 vs 리다이렉트 (0) | 2025.03.14 |