#!/bin/bash
PROJECT_NAME=$1
OWNER_NAME=userName
TEMPORARY=/home/$OWNER_NAME/deployProjectWar
REPOSITORY=/home/$OWNER_NAME/$PROJECT_NAME/bin
BACKUPDIR="$REPOSITORY/backup/"`date "+%Y%m%d_%H%M%S"`
echo $PROJECT_NAME
if [ -z $PROJECT_NAME ]; then
echo "> 프로젝트명을 입력하세요."
exit 0
fi
echo "> Build 파일 백업"
cd $REPOSITORY/
mkdir -p ${BACKUPDIR}
cp -v ../bin/*.war ${BACKUPDIR}/
echo "> Build 파일 복사"
if [ -z $TEMPORARY/$PROJECT_NAME*.war ]; then
echo $PROJECT_NAME " WAR 프로젝트가 없습니다."
exit 0
fi
cp -v $TEMPORARY/$PROJECT_NAME*.war $REPOSITORY/
echo "> 현재 구동중인 애플리케이션 pid 확인"
CURRENT_PID=$(pgrep -fl $PROJECT_NAME | grep java | awk '{print $1}')
if [ -z $CURRENT_PID ]; then
echo "> 현재 구동중인 애플리케이션이 없으므로 종료하지 않습니다."
else
echo "> kill -9 $CURRENT_PID"
kill -9 $CURRENT_PID
sleep 3
fi
echo "> 새 어플리케이션 배포"
JAR_NAME=$(ls -tr $REPOSITORY/$PROJECT_NAME*.war | tail -n 1)
echo "> $JAR_NAME에 실행권한 추가"
chmod +x $JAR_NAME
echo "> $JAR_NAME 실행"
nohup java -jar $JAR_NAME > $REPOSITORY/nohup.out 2>&1 &