api-game.sh
2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# !/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=(60080)
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