forked from didi/falcon-log-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol
executable file
·141 lines (128 loc) · 2.61 KB
/
control
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
#!/bin/bash
workspace=$(cd $(dirname $0) && pwd)
cd $workspace
module=log-agent
app=falcon-$module
conf=cfg/cfg.json
strategyconf=cfg/strategy.json
pidfile=var/app.pid
logfile=log/app.log
mkdir -p var &>/dev/null
mkdir -p log &>/dev/null
function install() {
stop_old
for i in `seq 1 10`
do
pidof log-analysis | grep -q '[0-9]' || echo 'old log-analysis stop success'; return 0
stop_old
sleep 1
done
echo 'install error'
}
function stop_old() {
/usr/local/sbin/monit stop log-analysis
monit stop log-analysis
}
function stop_pid() {
pid=$(cat $pidfile)
# 校验pid与进程名是否一致 || 根据进程名获取pid,要求父进程ppid==1(适用于root启动的agent)
ps -p ${pid} | grep -q ${app} || pid=$(ps -o ppid,pid $(pidof ${app}) | awk '$1==1 {print $2}')
if [ -n ${pid} ]; then
kill ${pid} || kill -9 ${pid}
fi
echo "$app stoped... or not start"
}
function start() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "[old process]still running, quit, pid = "
cat $pidfile
else
nohup ./$app -c $conf -s $strategyconf>>$logfile 2>&1 &
echo $! > $pidfile
echo "start ok, pid=$!"
fi
}
function stop() {
stop_old
kill `get_pid`
for i in `seq 1 20`
do
sleep 1
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "[restart]still running, wait.. , pid = "
cat $pidfile
if [ $i -gt 10 ]
then
kill -9 `get_pid`
fi
continue
else
echo "stoped"
break
fi
done
}
function shutdown() {
pid=`get_pid`
kill -9 $pid
echo "stoped"
}
function restart() {
stop
sleep 1
start
}
function status() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "running, pid="
cat $pidfile
else
echo "stoped"
fi
}
function get_pid() {
if [ -f $pidfile ];then
cat $pidfile
else
pid=`ps aux | grep $app | grep $conf | awk '{print $2}'`
if [ "x_$pid" != "x_" ]; then
echo $pid > $pidfile
cat $pidfile
fi
fi
}
function check_pid() {
pid=`get_pid`
if [ "x_" != "x_$pid" ]; then
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
return $running
fi
return 0
}
action=$1
case $action in
"install" )
install
;;
"start" )
start
;;
"stop" )
stop
;;
"kill" )
shutdown
;;
"restart" )
restart
;;
"status" )
status
;;
esac