uninstall_debian.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SERVICE_NAME="vmess-domain-rotator"
  4. APP_DIR="/opt/vmess-domain-rotator"
  5. KEEP_APP_DIR="0"
  6. REMOVE_USER="0"
  7. RUN_USER="vmessrotator"
  8. usage() {
  9. cat <<'EOF'
  10. Usage: sudo bash scripts/uninstall_debian.sh [options]
  11. Options:
  12. --app-dir <path> Install directory (default: /opt/vmess-domain-rotator)
  13. --service-name <name> Service base name (default: vmess-domain-rotator)
  14. --keep-app-dir Keep install directory and files
  15. --remove-user <name> Remove this service user after uninstall
  16. -h, --help Show help
  17. Examples:
  18. sudo bash scripts/uninstall_debian.sh
  19. sudo bash scripts/uninstall_debian.sh --keep-app-dir
  20. sudo bash scripts/uninstall_debian.sh --remove-user vmessrotator
  21. EOF
  22. }
  23. while [[ $# -gt 0 ]]; do
  24. case "$1" in
  25. --app-dir)
  26. APP_DIR="$2"
  27. shift 2
  28. ;;
  29. --service-name)
  30. SERVICE_NAME="$2"
  31. shift 2
  32. ;;
  33. --keep-app-dir)
  34. KEEP_APP_DIR="1"
  35. shift
  36. ;;
  37. --remove-user)
  38. REMOVE_USER="1"
  39. RUN_USER="$2"
  40. shift 2
  41. ;;
  42. -h|--help)
  43. usage
  44. exit 0
  45. ;;
  46. *)
  47. echo "Unknown option: $1" >&2
  48. usage
  49. exit 1
  50. ;;
  51. esac
  52. done
  53. if [[ "$(id -u)" -ne 0 ]]; then
  54. echo "Please run as root (use sudo)." >&2
  55. exit 1
  56. fi
  57. if systemctl list-unit-files | grep -q "^${SERVICE_NAME}.timer"; then
  58. systemctl disable --now "${SERVICE_NAME}.timer" || true
  59. fi
  60. if systemctl list-unit-files | grep -q "^${SERVICE_NAME}.service"; then
  61. systemctl stop "${SERVICE_NAME}.service" || true
  62. fi
  63. rm -f "/etc/systemd/system/${SERVICE_NAME}.service"
  64. rm -f "/etc/systemd/system/${SERVICE_NAME}.timer"
  65. systemctl daemon-reload
  66. systemctl reset-failed
  67. if [[ "$KEEP_APP_DIR" != "1" ]]; then
  68. rm -rf "$APP_DIR"
  69. fi
  70. if [[ "$REMOVE_USER" == "1" ]]; then
  71. if id -u "$RUN_USER" >/dev/null 2>&1; then
  72. userdel "$RUN_USER" || true
  73. fi
  74. fi
  75. echo "Uninstall completed."
  76. if [[ "$KEEP_APP_DIR" == "1" ]]; then
  77. echo "Kept app directory: ${APP_DIR}"
  78. else
  79. echo "Removed app directory: ${APP_DIR}"
  80. fi