프로그램/LINUX

[Linux] Shell script 만들기

프뚜 2022. 7. 25. 16:12
728x90
반응형
SMALL

안녕하세요!

프뚜(프로그래머 뚜)입니다!

 

[개발 환경]

 - OS: CentOS 8 (docker container)

 - ROOT 계정으로 실행

 - 인터넷이 가능한 환경

 

개발을 하다보면 하나의 script로 여러 개의 이벤트를 처리하고 싶을 때가 있습니다.

예) spring  boot embedded tomcat을 사용하고 있을 때, 프로세스 중 java를 종료하고 새로운 java파일을 실행하기

 

 

1. script 생성하기

vi ssjeong.sh

 

 

2. 프로세스 확인 후 해당 프로세스를 종료하기

# JAVA Process를 확인
PID=$(ps -ef | grep 'java' | grep -v 'grep' | awk '{print $2}')
echo "Process ID: $PID"

# 있다면 종료
if [ -z $PID ]; then
        echo "No process is running"
else
        echo "Kill process"
        kill -9 $PID
fi

 

 

3. 새로운 java를 실행하기

# Log 제거
rm -rf /home/ssjeong/ssjeong.log
echo "Server ssjeong log delete"

# Server Start 및 Log File 생성
nohup java -jar -Dfile.encoding=UTF-8 -Dspring.profiles.active=local /home/ssjeong/ssjeong.jar > /home/ssjeong/ssjeong.log 2>&1&
echo "Server Start"

 

 

4. ssjeong.sh 전체 소스

# Java Process를 확인
PID=$(ps -ef | grep 'java' | grep -v 'grep' | awk '{print $2}')
echo "Process ID: $PID"

# 있다면 모두 종료
if [ -z $PID ]; then
        echo "No process is running"
else
        echo "Kill process"
        kill -9 $PID
fi

# Log 제거
rm -rf /home/ssjeong/ssjeong.log
echo "Server ssjeong log delete"

# Server Start 및 Log File 생성
nohup java -jar -Dfile.encoding=UTF-8 -Dspring.profiles.active=local /home/ssjeong/ssjeong.jar > /home/ssjeong/ssjeong.log 2>&1&
echo "Server Start"

# Script 종료
exit 0

 

 

궁금하신 부분 또는 문제가 생긴 부분에 대해서 댓글 남겨주시면 빠르고 정확한 답변드리겠습니다.

728x90
반응형
LIST