-
Notifications
You must be signed in to change notification settings - Fork 47
/
k-install.sh
255 lines (202 loc) · 5.8 KB
/
k-install.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/bash
# Author: haoxie666
# github: https://github.com/haoxie666
DOWNLOAD_HOST=""
AMD64_HOST="https://cdn.jsdelivr.net/gh/haoxie666/HXMinerProxy/kenc/kenc_linux_amd64"
ARM64_HOST="https://cdn.jsdelivr.net/gh/haoxie666/HXMinerProxy/kenc/kenc_linux_arm64"
ARM386_HOST="https://cdn.jsdelivr.net/gh/haoxie666/HXMinerProxy/kenc/kenc_arm386"
PATH_KENC="/root/kenc"
PATH_EXEC="kenc"
CUR_PATH=$(cd "$(dirname "$0")"; pwd)
TASK_COMMAND="nohup ${PATH_KENC}/${PATH_EXEC} &"
CRONTAB_TASK="@reboot ${TASK_COMMAND}"
CRONTAB_BAK_FILE="${CUR_PATH}/crontab_bak"
#######color code########
RED="31m"
GREEN="32m"
YELLOW="33m"
BLUE="36m"
FUCHSIA="35m"
colorEcho(){
COLOR=$1
echo -e "\033[${COLOR}${@:2}\033[0m"
}
checkProcess() {
COUNT=$(ps -ef |grep $1 |grep -v "grep" |wc -l)
if [ $COUNT -eq 0 ]; then
return 0
else
return 1
fi
}
uninstall() {
stop
rm -rf ${PATH_KENC}
turn_off
colorEcho $GREEN "卸载完成"
}
filterResult() {
if [ $1 -eq 0 ]; then
echo ""
else
colorEcho ${RED} "【${2}】失败。"
if [ ! $3 ];then
colorEcho ${RED} "!!!!!!!!!!!!!!!ERROR!!!!!!!!!!!!!!!!"
exit 1
fi
fi
echo -e
}
turn_on() {
echo '设置开机启动...'
crontab -l > ${CRONTAB_BAK_FILE} 2>/dev/null
sed -i "/.*${TASK_COMMAND}/d" ${CRONTAB_BAK_FILE}
echo "${CRONTAB_TASK}" >> ${CRONTAB_BAK_FILE}
crontab ${CRONTAB_BAK_FILE}
echo '开机启动设置完毕'
}
turn_off() {
echo 'Delete crontab task...'
crontab -l > ${CRONTAB_BAK_FILE} 2>/dev/null
sed -i "/.*${SCRIPT_NAME}/d" ${CRONTAB_BAK_FILE}
crontab ${CRONTAB_BAK_FILE}
echo 'Complete'
}
[ $(id -u) != "0" ] && { colorEcho ${RED} "请使用root用户执行此脚本."; exit 1; }
installapp() {
checkProcess "kenc"
if [ $? -eq 1 ]; then
colorEcho ${RED} "发现正在运行的KENC, 需要停止才可继续安装。"
colorEcho ${YELLOW} "输入1停止正在运行的KENC并且继续安装, 输入2取消安装。"
read -p "$(echo -e "请选择[1-2]:")" choose
case $choose in
1)
stop
;;
2)
echo "取消安装"
return
;;
*)
echo "输入错误, 取消安装。"
return
;;
esac
fi
colorEcho $BLUE "创建目录"
if [[ ! -d $PATH_KENC ]];then
mkdir $PATH_KENC
chmod 777 -R $PATH_KENC
else
colorEcho $YELLOW "目录已存在, 无需重复创建, 继续执行安装。"
fi
colorEcho $BLUE "拉取程序"
wget -P $PATH_KENC "${DOWNLOAD_HOST}" -O "${PATH_KENC}/${PATH_EXEC}" 1>/dev/null
filterResult $? "拉取程序 ${DOWNLOAD_HOST}"
chmod 777 -R "${PATH_KENC}/${PATH_EXEC}"
change_limit
turn_on
start
}
start() {
colorEcho $BLUE "启动程序..."
checkProcess "kenc"
if [ $? -eq 1 ]; then
colorEcho ${RED} "程序已经启动,请不要重复启动。"
return
else
# 要先cd进去 否则nohup日志会产生在当前路径
cd $PATH_KENC
filterResult $? "打开目录"
nohup "${PATH_KENC}/${PATH_EXEC}" &
filterResult $? "启动程序"
colorEcho $GREEN "|----------------------------------------------------------------|"
colorEcho $GREEN "程序启动成功, WEB访问端口3101"
colorEcho $GREEN "|----------------------------------------------------------------|"
fi
}
stop() {
colorEcho $GREEN "停止运行kenc"
kill -9 $(pidof kenc)
}
restart() {
stop
sleep 1
start
}
change_limit(){
colorEcho $BLUE "修改系统最大连接数"
changeLimit="n"
if [ $(grep -c "root soft nofile" /etc/security/limits.conf) -eq '0' ]; then
echo "root soft nofile 65535" >>/etc/security/limits.conf
echo "* soft nofile 65535" >>/etc/security/limits.conf
changeLimit="y"
fi
if [ $(grep -c "root hard nofile" /etc/security/limits.conf) -eq '0' ]; then
echo "root hard nofile 65535" >>/etc/security/limits.conf
echo "* hard nofile 65535" >>/etc/security/limits.conf
changeLimit="y"
fi
if [ $(grep -c "DefaultLimitNOFILE=65535" /etc/systemd/user.conf) -eq '0' ]; then
echo "DefaultLimitNOFILE=65535" >>/etc/systemd/user.conf
changeLimit="y"
fi
if [ $(grep -c "DefaultLimitNOFILE=65535" /etc/systemd/system.conf) -eq '0' ]; then
echo "DefaultLimitNOFILE=65535" >>/etc/systemd/system.conf
changeLimit="y"
fi
if [[ "$changeLimit" = "y" ]]; then
echo "连接数限制已修改为65535,重启服务器后生效"
else
echo -n "当前连接数限制:"
ulimit -n
fi
}
echo "-------------------------------------------------------"
colorEcho ${GREEN} "欢迎使用KENC安装工具, 请输入操作号继续。"
echo "项目地址:https://github.com/haoxie666/HxMinerProxy"
echo ""
echo "1、安装KENC(amd64)"
echo "2、安装KENC(arm64)"
echo "3、安装KENC(386)"
echo "4、重启KENC"
echo "5、停止KENC"
echo "6、卸载KENC"
echo "7、设置开机启动"
echo "8、关闭开机启动"
echo ""
colorEcho ${YELLOW} "如果在此之前是手动安装的程序,请自己手动退出程序后再执行此脚本,否则容易发生冲突,所有操作尽量通过此脚本完成。"
echo "-------------------------------------------------------"
read -p "$(echo -e "请选择[1-3]:")" choose
case $choose in
1)
DOWNLOAD_HOST=$AMD64_HOST
installapp "amd64"
;;
2)
DOWNLOAD_HOST=$ARM64_HOST
installapp "arm64"
;;
3)
DOWNLOAD_HOST=$ARM386_HOST
installapp "386"
;;
4)
restart
;;
5)
stop
;;
6)
uninstall
;;
7)
turn_on
;;
8)
turn_off
;;
*)
echo "输入了错误的指令, 请重新输入。"
;;
esac