api-game.sh 2.91 KB
 # !/bin/bash
if [ ! -n "$1" ]; then
   echo "params error"
   exit 1
elif [ $1 != "start" -a $1 != "restart" -a $1 != "stop" ]; then
   echo "please input start|stop|restart"
   exit 2
fi
env=pre
nowdate=`date +"%Y%m%d%H%M"`
execdir=/home/exec/apigame/api/$nowdate
codedir=/home/code/go/apigame/src
execname=apigame
execnewname=API_GAME_$nowdate
ports=(39080)
codeport=80
echo "${execname} script is begin...."
sleep 1s
# cd $codedir
for i in ${ports[@]};
do
    if [ $1 == "start" ];then
        userport=`netstat -antp | grep ":${i}"`
        if [ ${#userport} -gt 3 ]; then
                echo "port ${i} is busy..."
                break
        fi
    fi
    if [ $1 == "start" -o $1 == "restart" ];then
        #copy new main 
        cd $codedir
        if [ $? -ne 0 ]; then
                echo "cd ${codedir} error,port:${i}"
                break
        fi
        execdirpath=$execdir/$i
        if [ ! -d $execdirpath ]; then
                mkdir -p $execdirpath
                if [ $? -ne 0 ]; then
                echo "mkdir ${execdirpath} error"
                 break
                fi
                cp $execname $execdirpath/${execnewname}_${i}
                cp -r conf $execdirpath/
                #cp -r ../conf $execdirpath/../
                sed -i "s#httpport = ${codeport}#httpport = ${i}#g" $execdirpath/conf/${env}.conf
                if [ $? -ne 0 ]; then
                        echo "sed httpport ${codeport},i:${i} error"
                        break
                fi
                sed -i "s#appname = ${execname}#appname = ${execnewname}_${i}#g" $execdirpath/conf/${env}.conf
                if [ $? -ne 0 ]; then
                        echo "sed appname ${execname},i:${i} error"
                        break
                fi  
                rm -rf app.conf
                if [ $? -ne 0 ]; then
                        echo "delete app.conf i:${i} error"
                        break
                fi  
                echo "include \"${env}.conf\"" > $execdirpath/conf/app.conf
        fi
    fi
    #kill old port main
    if [ $1 == "stop" -o $1 == "restart" ];then
        userport=`netstat -antp | grep ":${i}"`
        if [ ${#userport} -gt 3 ]; then
                kill -9 $(netstat -nlp | grep :${i} | awk '{print $7}' | awk -F"/" '{ print $1 }')
                if [ $? -ne 0 ]; then
                        echo "kill port ${i} success"
                        break
                else
                        echo "kill port ${i} success"
                fi  
        fi
    fi
    #start new maini
    if [ $1 == "start" -o $1 == "restart" ];then
        cd $execdirpath/
        nohup ./${execnewname}_${i} >/dev/null 2>$execdirpath/error.log  2>&1 &
        if [ $? -ne 0 ]; then
                echo "${execnewname}_${i} start error "
                break
        else
                echo "${execnewname}_${i} start ok "
        fi
    fi
    sleep 5s
done
echo "${execname} script is end...."
exit 0