forked from didi/falcon-log-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol
executable file
·112 lines (102 loc) · 2.13 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
#!/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 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() {
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 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
"start" )
start
;;
"stop" )
stop
;;
"restart" )
restart
;;
"status" )
status
;;
esac