-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathctrl.sh
executable file
·144 lines (120 loc) · 2.18 KB
/
ctrl.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
#! /bin/bash
WORKSPACE=$(cd $(dirname $0)/; pwd)
cd $WORKSPACE
### config ###
APPS="ipqueryd"
INIT_DIRS="logs tmp"
### common function ###
function _make_pid_file() {
app=$1
echo "$WORKSPACE/tmp/$app.pid"
}
function _get_pid() {
app=$1
pid_file=`_make_pid_file $app`
if [ -f $pid_file ]; then
pid=`cat $pid_file`
kill -0 $pid >/dev/null 2>&1
if [ $? = 0 ]; then
echo $pid
else
rm -f $pid_file
echo 0
fi
else
echo 0
fi
}
### action ###
function useage() {
echo "Useage: $0 \$cmd [\$app]";
exit 1;
}
function init() {
app=$1
for dir in $INIT_DIRS ; do
init_path=$WORKSPACE/$dir
if [ ! -d $init_path ]; then
mkdir -p $init_path && chmod -R 777 $init_path
fi
done
}
function build() {
app=$1
cd $WORKSPACE/src
./go.sh install main/$app
cd $WORKSPACE
}
function status() {
app=$1
pid=`_get_pid $app`
if [ "$pid" = 0 ]; then
echo "$app not running."
else
echo "$app with pid $pid is running."
fi
}
function start() {
app=$1
pid=`_get_pid $app`
if [ $pid -gt 0 ]; then
echo "$app with pid $pid has running!"
return 1
fi
($WORKSPACE/bin/$app -c $WORKSPACE/conf/$app.json >> $WORKSPACE/logs/$app.log 2>&1 &)
}
function stop() {
app=$1
pid=`_get_pid $app`
if [ $pid -eq 0 ]; then
echo "$app not running!"
return 1
fi
kill -9 $pid
rm -f `_make_pid_file $app`
}
function restart() {
app=$1
stop $app && start $app
}
function run() {
app=$1
init $app
build $app
start $app
}
### main ###
APP_LIST=($APPS)
APP=${APP_LIST[0]}
for (( i = 0; i < ${#APP_LIST[@]}; i++ )); do
app=${APP_LIST[$i]}
if [ $app = "$2" ]; then
APP=$app
fi
done
case $1 in
"init")
init $APP
;;
"build")
build $APP
;;
"status")
status $APP
;;
"start")
start $APP
;;
"stop")
stop $APP
;;
"restart")
restart $APP
;;
"run")
run $APP
;;
*)
useage
;;
esac