-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.sh
102 lines (94 loc) · 3.18 KB
/
bot.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
#!/bin/bash
# 定义变量
binName="keqing-bot"
LOG_PATH="./out.log"
# 若是输入格式不对,给出提示!
tips() {
echo ""
echo "WARNING!!!......Tips, please use command: sh auto_deploy.sh [start|stop|restart|status]. For example: sh auto_deploy.sh start "
echo ""
exit 1
}
# 启动方法
start() {
# 从新获取一下pid,由于其它操做如stop会致使pid的状态更新
pid=$(ps -ef | grep $binName | grep -v grep | awk '{print $2}')
# -z 表示若是$pid为空时执行
if [ -z $pid ]; then
nohup yarn dev >>$LOG_PATH 2>&1 &
pid=$(ps -ef | grep $binName | grep -v grep | awk '{print $2}')
echo ""
echo "Service ${binName} is starting!pid=${pid}"
echo "........................Here is the log.............................."
echo "....................................................................."
# tail -f $LOG_PATH
echo "........................Start successfully!........................."
else
echo ""
echo "Service ${binName} is already running,it's pid = ${pid}. If necessary, please use command: sh auto_deploy.sh restart."
echo ""
fi
}
# 中止方法
stop() {
# 从新获取一下pid,由于其它操做如start会致使pid的状态更新
pid=$(ps -ef | grep $binName | grep -v grep | awk '{print $2}')
# -z 表示若是$pid为空时执行。 注意:每一个命令和变量之间必定要先后加空格,不然会提示command找不到
if [ -z $pid ]; then
echo ""
echo "Service ${binName} is not running! It's not necessary to stop it!"
echo ""
else
kill -9 $pid
echo ""
echo "Service stop successfully!pid:${pid} which has been killed forcibly!"
echo ""
fi
}
# 输出运行状态方法
status() {
# 从新获取一下pid,由于其它操做如stop、restart、start等会致使pid的状态更新
pid=$(ps -ef | grep $binName | grep -v grep | awk '{print $2}')
# -z 表示若是$pid为空时执行。注意:每一个命令和变量之间必定要先后加空格,不然会提示command找不到
if [ -z $pid ]; then
echo ""
echo "Service ${binName} is not running!"
echo ""
else
echo ""
echo "Service ${binName} is running. It's pid=${pid}"
echo ""
fi
}
# 重启方法
restart() {
echo ""
echo ".............................Restarting.............................."
echo "....................................................................."
# 从新获取一下pid,由于其它操做如start会致使pid的状态更新
pid=$(ps -ef | grep $binName | grep -v grep | awk '{print $2}')
# -z 表示若是$pid为空时执行。 注意:每一个命令和变量之间必定要先后加空格,不然会提示command找不到
if [ ! -z $pid ]; then
kill -9 $pid
fi
start
echo "....................Restart successfully!..........................."
}
# 根据输入参数执行对应方法,不输入则执行tips提示方法
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
tips
;;
esac