aws/EC2

nuxt 자동베포 쉘스크립트

디비드킴 2022. 12. 12. 16:51
value=0
value2=0
echo > /dev/tcp/127.0.0.1/3032
value=`echo $?`
echo > /dev/tcp/127.0.0.1/3031
value2=`echo $?`

echo "3032오픈상황:${value}"
echo "3031오픈상황:${value2}"

timestamp=`date +%Y%m%d%H%M%C`
echo "$timestamp"
cd /home/ec2-user/
mv nuxt ${timestamp}

port=0;
upport=0;
if [ ${value} -eq 0 ]
then
        echo '3031시작'
        sed -i 's/3032/3031/g'  /home/ec2-user/${timestamp}/nuxt.config.js
        sed -i 's/3032/3031/g'  /etc/nginx/nginx.conf
        port=3032
        upport=3031
else
        sed -i 's/3031/3032/g'  /home/ec2-user/${timestamp}/nuxt.config.js
        sed -i 's/3031/3032/g'  /etc/nginx/nginx.conf
        echo '3032시작'
        port=3031
        upport=3032
fi

cd /home/ec2-user/${timestamp}
npm install
npm run build
sudo nohup npm start &

while :
        do
                echo > /dev/tcp/127.0.0.1/$upport;
                stop=`echo $?`;
                echo "실행중";
                if [ $stop -eq 0 ]; then
                        break;

                else
                        echo "아직실행되지 않았습니다 10초sleep";
                        sleep 10
                fi
        done

        echo '실행완료';
        sudo nginx -s reload

        echo "종료할 포트:${port}"
        result_value=$(netstat -nap 2>/dev/null | grep ${port} | awk '{print $7}')
        number_value=${#result_value}
        if [ $number_value == 0 ]; then
                echo '해당포트가 미사용중으로 이미 종료되었습니다..'
        else
                echo '종료를 시작합니다.'
                pid_val=${result_value%%'/'*}
                echo "종료 예정포트 ${pid_val}"
                sudo kill -9 $pid_val
                echo `종료포트결과,sudo lsof -i :${port}`
        fi