-
Notifications
You must be signed in to change notification settings - Fork 9
/
system-cleanup-script.sh
92 lines (77 loc) · 1.83 KB
/
system-cleanup-script.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
#!/bin/bash
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RESET='\033[0m'
# 辅助函数
red() {
echo -e "${RED}$1${RESET}"
}
green() {
echo -e "${GREEN}$1${RESET}"
}
yellow() {
echo -e "${YELLOW}$1${RESET}"
}
# 清理cron任务
cleanCron() {
echo "" > null
crontab null
rm null
}
# 结束所有用户进程
killUserProc() {
local user=$(whoami)
pkill -kill -u $user
}
# 系统初始化函数
initServer() {
read -p "$(red "确定要初始化系统吗?这将删除大部分数据。 [y/n] [n]: ")" input
input=${input:-n}
if [[ "$input" == "y" ]] || [[ "$input" == "Y" ]]; then
read -p "是否保留用户配置?[y/n] [y]: " saveProfile
saveProfile=${saveProfile:-y}
green "清理cron任务..."
cleanCron
green "清理用户进程..."
killUserProc
green "清理磁盘..."
if [[ "$saveProfile" = "y" ]] || [[ "$saveProfile" = "Y" ]]; then
rm -rf ~/* 2>/dev/null
else
rm -rf ~/* ~/.* 2>/dev/null
fi
yellow "系统初始化完成"
else
yellow "操作已取消"
fi
}
# 显示菜单
showMenu() {
clear
echo "========================================="
echo " 系统清理脚本 - SSH面板 "
echo "========================================="
echo "1. 初始化系统(清理数据)"
echo "2. 退出"
echo "========================================="
read -p "请选择操作 [1-2]: " choice
case $choice in
1)
initServer
;;
2)
echo "退出脚本"
exit 0
;;
*)
red "无效的选择,请重新输入"
;;
esac
}
# 主循环
while true; do
showMenu
read -p "按Enter键继续..."
done