-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.sh
executable file
·172 lines (158 loc) · 4 KB
/
service.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
if [ $# -lt 1 ];
then
echo "USAGE: $0 classname opts"
exit 1
fi
BASE_DIR=$(dirname $0)
# Returns 0 if the process with PID $1 is running.
function checkProcessIsRunning {
local pid="$1"
# echo "check pid:$1"
if test $( ps -ef | grep $pid | grep python | wc -l ) -lt 1 ; then return 1; fi
return 0;
}
# Returns 0 when the service is running and sets the variable $pid to the PID.
function getServicePID {
if [ ! -f $PID_FILE ]; then return 1; fi
pid="$(<$PID_FILE)"
checkProcessIsRunning $pid || return 1
return 0; }
function startServiceProcess {
touch $PID_FILE
rm -rf $current_absolute_path/nohup.log
nohup $KEY_WORDS >> $current_absolute_path/nohup.log 2>&1 & echo $! > $PID_FILE
sleep 0.1
pid="$(<$PID_FILE)"
# echo $pid
if checkProcessIsRunning $pid; then :; else
echo "$SERVICE_NAME start failed, see $current_absolute_path/nohup.log."
return 1
fi
return 0;
}
function stopServiceProcess {
STOP_DATE=`date +%Y%m%d%H%M%S`
kill $pid || return 1
# echo “stop 。。。。。。”
for ((i=0; i<10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
#echo "正在删除$PID_FILE"
rm -f $PID_FILE
return 0
fi
sleep 1
done
echo "\n$SERVICE_NAME did not terminate within 10 seconds, sending SIGKILL..."
kill -s KILL $pid || return 1
local killWaitTime=15
for ((i=0; i<10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $PID_FILE
return 0
fi
sleep 1
done
echo "Error: $SERVICE_NAME could not be stopped within 10 + 10 seconds!"
return 1;
}
function startService {
getServicePID
if [ $? -eq 0 ]; then echo "$SERVICE_NAME is already running"; RETVAL=0; return 0; fi
echo -n "Starting $SERVICE_NAME..."
startServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
COUNT=0
while [ $COUNT -lt 1 ]; do
for (( i=0; i<5; i=i+1 )) do
STR=`grep "has started" $current_absolute_path/nohup.log`
if [ ! -z "$STR" ]; then
echo "PID=$pid\n"
echo "Server start OK in $i seconds."
break;
fi
echo -e ".\c"
sleep 1
done
break
done
echo "OK!"
#echo "PID: $START_PIDS"
# echo "started PID=$pid"
RETVAL=0
return 0;
}
function stopService {
getServicePID
if [ $? -ne 0 ]; then echo -n "$SERVICE_NAME is not running"; RETVAL=0; echo ""; return 0; fi
echo "Stopping $SERVICE_NAME... "
stopServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0;
}
function checkServiceStatus {
echo -n "Checking for $SERVICE_NAME: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0;
}
function main {
RETVAL=0
case "$1" in
start)
stopService && startService
;;
stop)
stopService
;;
restart)
stopService && startService
;;
status)
checkServiceStatus
;;
*)
echo "Usage: $0 {service.sh start|stop|restart|status capture|copyserver|sender}"
exit 1
;;
esac
exit $RETVAL
}
function preOpration {
#get absolute path
current_absolute_path=`pwd`
case "$1" in
capture )
cd $current_absolute_path/capture
KEY_WORDS="python -u start_capture.py"
SERVICE_NAME="capture"
;;
copyserver )
cd $current_absolute_path/copyserver
KEY_WORDS="python -u start_copy.py"
SERVICE_NAME="copyserver"
;;
sender )
cd $current_absolute_path/sender
KEY_WORDS="python -u start_sender.py"
SERVICE_NAME="sender"
;;
*)
echo "Usage: $0 {service.sh start|stop|restart|status capture|copyserver|sender}"
exit 1
;;
esac
PID_FILE="$SERVICE_NAME.pid"
}
preOpration $2
main $1
cd $current_absolute_path