uninstall.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/bash
  2. # 资源占用显示系统卸载脚本
  3. set -e
  4. # 颜色定义
  5. RED='\033[0;31m'
  6. GREEN='\033[0;32m'
  7. YELLOW='\033[1;33m'
  8. NC='\033[0m' # No Color
  9. # 打印函数
  10. print_info() {
  11. echo -e "${GREEN}[INFO]${NC} $1"
  12. }
  13. print_warn() {
  14. echo -e "${YELLOW}[WARN]${NC} $1"
  15. }
  16. print_error() {
  17. echo -e "${RED}[ERROR]${NC} $1"
  18. }
  19. # 检查是否为 root 用户
  20. check_root() {
  21. if [[ $EUID -ne 0 ]]; then
  22. print_error "此脚本需要 root 权限运行"
  23. exit 1
  24. fi
  25. }
  26. # 停止并禁用服务
  27. stop_service() {
  28. print_info "停止并禁用 swb-sys-monitor 服务..."
  29. if systemctl is-active --quiet swb-sys-monitor 2>/dev/null; then
  30. systemctl stop swb-sys-monitor
  31. print_info "服务已停止"
  32. else
  33. print_info "服务未运行"
  34. fi
  35. if systemctl is-enabled --quiet swb-sys-monitor 2>/dev/null; then
  36. systemctl disable swb-sys-monitor
  37. print_info "服务已禁用"
  38. else
  39. print_info "服务未启用"
  40. fi
  41. }
  42. # 删除系统服务文件
  43. remove_service() {
  44. print_info "删除系统服务文件..."
  45. if [[ -f /etc/systemd/system/swb-sys-monitor.service ]]; then
  46. rm -f /etc/systemd/system/swb-sys-monitor.service
  47. systemctl daemon-reload
  48. print_info "系统服务文件已删除"
  49. else
  50. print_info "系统服务文件不存在"
  51. fi
  52. }
  53. # 删除二进制文件
  54. remove_binary() {
  55. print_info "删除二进制文件..."
  56. if [[ -f /usr/local/bin/swb-sys-monitor ]]; then
  57. rm -f /usr/local/bin/swb-sys-monitor
  58. print_info "二进制文件已删除"
  59. else
  60. print_info "二进制文件不存在"
  61. fi
  62. }
  63. # 清理日志文件(可选)
  64. clean_logs() {
  65. read -p "是否要清理服务日志?[y/N]: " -n 1 -r
  66. echo
  67. if [[ $REPLY =~ ^[Yy]$ ]]; then
  68. print_info "清理服务日志..."
  69. journalctl --vacuum-time=1d -u swb-sys-monitor 2>/dev/null || true
  70. print_info "日志已清理"
  71. fi
  72. }
  73. # 确认卸载
  74. confirm_uninstall() {
  75. echo
  76. print_warn "即将卸载资源占用显示系统"
  77. print_warn "这将删除:"
  78. echo " - 系统服务文件"
  79. echo " - 二进制文件"
  80. echo " - 可选:服务日志"
  81. echo
  82. read -p "确认卸载?[y/N]: " -n 1 -r
  83. echo
  84. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  85. print_info "卸载已取消"
  86. exit 0
  87. fi
  88. }
  89. # 主函数
  90. main() {
  91. print_info "开始卸载资源占用显示系统..."
  92. check_root
  93. confirm_uninstall
  94. stop_service
  95. remove_service
  96. remove_binary
  97. clean_logs
  98. print_info "卸载完成!"
  99. echo
  100. print_info "资源占用显示系统已从您的系统中移除"
  101. print_info "如需重新安装,请运行: sudo ./scripts/install.sh"
  102. }
  103. # 运行主函数
  104. main "$@"