-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtgbot.sh
177 lines (166 loc) · 3.4 KB
/
tgbot.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
173
174
175
176
177
#!/bin/bash
# description: Starts and stops HBcaobot.
# author: HBcao
rawpath=$(readlink $0);
if [ -z $rawpath ]; then
rawpath=$(pwd)/$(echo $0 | sed 's/^\.\///');
fi
root=$(dirname $rawpath);
bots=();
commands=();
cd ${root};
for i in *; do
if [ -d "$i" ] && [ -f "$i/.env" ]; then
commands[${#bots[@]}]="python3 ${root}/main.py $i";
bots[${#bots[@]}]=$i;
fi
done
if [ ${#bots[@]} = 0 ] && [ -f ".env" ]; then
bots=(.);
fi
if [ ${#bots[@]} = 0 ]; then
echo "当前根目录下缺少 .env 文件, 或存在 .env 文件的目录"
exit 1
fi
choose() {
for i in ${!bots[@]}; do
printf "[%s] %s\n" $i ${bots[$i]};
done
printf '请输入序号: '
}
start(){
if [ -z $1 ]; then
echo 'Error Args'
exit 1
fi
index=$1
echo "${bots[$index]} 启动中..."
nohup python3 ${root}/main.py "${bots[$index]}" > ${root}/${bots[$index]}/bot.log 2>&1 &
}
status(){
for i in ${!bots[@]}; do
color=("\E[01m\E[31m" "\E[01m\E[32m");
statusText=("inactive (dead)" "active (running)");
status=();
pid=();
since=();
ago=();
k=${commands[$i]};
echo $k;
count=`ps -ax | grep "$k" | grep -v grep | grep -v bin | wc -l`;
if [[ $count -ne 0 ]];then
status[$i]=1;
pid[$i]=`ps -ax | grep "$k" | grep -v grep | awk -F " " 'NR==1{print $1}'`;
echo ${pid[$i]}
since[$i]=`ps -o lstart -p ${pid[$i]} | awk 'NR==2'`;
ago[$i]=`ps -o etime -p ${pid[$i]} | awk 'NR==2{print $1}'`;
fi
printf "%b●\E[0m $(basename $root)/%s \n" ${color[status[i]]} ${bots[i]};
printf "%10s: %b%s\E[0m; since %s, %s ago\n" "Active" "${color[status[i]]}" "${statusText[status[i]]}" "${since[i]}" "${ago[i]}";
printf "%10s: %s\n" \
"PID" "${pid[i]}" \
"Command" "${commands[i]}";
printf "\n";
done
}
stop(){
if [ -z $1 ]; then
echo 'Error Args'
exit 1
fi
index=$1
k=${commands[$index]}
count=`ps -ax | grep "$k" | grep -v grep | grep -v bin | grep -v sh | wc -l`;
if [[ $count -ne 0 ]];then
echo ${bots[$index]}'已启动,正在关闭...';
ps -ax | grep "$k" | grep -v grep | grep -v bin | grep -v sh | awk -F " " '{print $1}' | xargs -I {} kill {}
fi
}
arg1=$1
arg2=$2
case $arg2 in
start|restart|status|stop|log|cd)
t=$arg1
arg1=$arg2
arg2=$t
;;
esac
case $arg1 in
start)
index=""
if [ -n $arg2 ]; then
for i in ${!bots[@]}; do
if [ "${bots[$i]}" = "$arg2" ]; then
index=$i
fi
done
fi
if [ "$index" = "" ]; then
if [ ${#bots[@]} = 1 ]; then
index=0
else
choose
read index
fi
fi
stop $index
start $index
;;
restart)
if [ ${#bots[@]} = 1 ]; then
index=0
else
choose
read index
fi
stop $index
start $index
;;
stop)
if [ ${#bots[@]} = 1 ]; then
index=0
else
choose
read index
fi
stop $index
;;
status)
status
;;
log)
index=""
if [ -n $arg2 ]; then
for i in ${!bots[@]}; do
if [ "${bots[$i]}" = "$arg2" ]; then
index=$i
break
fi
done
fi
if [ "$index" = "" ]; then
ninput=$arg2
if [ ${#bots[@]} = 1 ]; then
index=0
else
choose
read index
fi
else
ninput=$3
fi
re='^[0-9]+$'
num=100
if [[ $ninput =~ $re ]];then
num=$ninput
fi
echo "查看 ${bots[$index]} 日志"
nl ${root}/${bots[$index]}/bot.log | tail -n $num
;;
cd)
cd ${root}
;;
*)
echo "Usage: $0 {start|restart|stop|status|log}"
exit 1
esac