-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_zip_gevent_server.sh
executable file
·127 lines (107 loc) · 2.85 KB
/
run_zip_gevent_server.sh
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/bin/bash
PYTHON_EGG_CACHE="/tmp/.PYTHON_EGG_CACHE_zipserver"
EXEC_USER="hejiarong"
LOG_DIR="/data/logs/zipserver"
LISTEN_IP="127.0.0.1"
BASE_PORT=8004
WORKER_COUNT=1
EXEC="/usr/bin/python2.7"
MAIN_FILE="./zipserver_gevent.py"
ARGS="1000"
STDOUT="./__gevent_wsgiserver.stdout"
DISP_NAME="gevent_wsgiserver"
PROC_SIG=${DISP_NAME}"_MdffDFEfsesf"
# Consts
RED='\e[1;91m'
GREN='\e[1;92m'
WITE='\e[1;97m'
NC='\e[0m'
# Global vailables
PROC_COUNT="0"
function count_proc()
{
PROC_COUNT=$(ps -ef | grep $MAIN_FILE | grep $PROC_SIG | grep -vc grep)
}
function list_proc()
{
ps -ef | grep -v grep | grep $PROC_SIG | grep --color $MAIN_FILE
}
function list_proc_pids()
{
ps -ef | grep $MAIN_FILE | grep $PROC_SIG | grep -v grep | awk '{print $2}'
}
function start_procs()
{
printf "Starting $DISP_NAME processes"
sudo /usr/sbin/useradd -M -s /bin/false $EXEC_USER 1>/dev/null 2>&1
sudo mkdir -p $LOG_DIR
sudo chown $EXEC_USER.$EXEC_USER $LOG_DIR
sudo chown $EXEC_USER.$EXEC_USER .
sudo mkdir -p $PYTHON_EGG_CACHE
sudo chmod 777 $PYTHON_EGG_CACHE
count_proc
if [ $PROC_COUNT \> 0 ]; then
echo
list_proc
echo -e ${RED}"\n[ERROR]" ${NC}"Start $DISP_NAME failed, processes already runing."
exit -1
fi
for ((PORT=$BASE_PORT; PORT<WORKER_COUNT+$BASE_PORT; PORT=PORT+1)); do
sudo -u $EXEC_USER PYTHON_EGG_CACHE=${PYTHON_EGG_CACHE} $EXEC $MAIN_FILE $LISTEN_IP $PORT $ARGS $PROC_SIG 1>$STDOUT 2>&1 &
echo $PORT
done
count_proc
while [ ${PROC_COUNT} -ne $WORKER_COUNT ]; do
printf "."
sleep 0.2
count_proc
done
echo -e ${GREN}"\n[OK]" ${NC}"$DISP_NAME start succesfully."
}
function stop_procs()
{
printf "Stoping $DISP_NAME"
count_proc
if [ ${PROC_COUNT} -eq 0 ]; then
echo -e ${RED}"\n[ERROR]" ${NC}"$DISP_NAME process not found."
exit -1
fi
kill -SIGKILL $(list_proc_pids)
count_proc
while [ ${PROC_COUNT} -ne 0 ]; do
printf "."
sleep 0.2
count_proc
done
echo -e ${GREN}"\n[OK]" ${NC}"$DISP_NAME stop succesfully."
}
function status_procs()
{
count_proc
echo -e ${RED}${PROC_COUNT}${NC} "$DISP_NAME processes runing."
}
MODE=${1}
case ${MODE} in
"start")
start_procs
;;
"stop")
stop_procs
;;
"restart")
stop_procs
start_procs
;;
"status")
status_procs
;;
*)
# usage
echo -e "\nUsage: $0 {start|stop|restart|status}"
echo -e ${WITE}" start "${NC}"Start $DISP_NAME processes."
echo -e ${WITE}" stop "${NC}"Kill all $DISP_NAME processes."
echo -e ${WITE}" restart "${NC}"Kill all $DISP_NAME processes and start again."
echo -e ${WITE}" status "${NC}"Show $DISP_NAME processes status.\n"
exit 1
;;
esac