Files
linux-bash/frp/stopkill_frp.sh
2026-02-17 17:28:37 +08:00

67 lines
1.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
# 强制停止 frp 服务并清理进程
# 用法curl -fsSL "https://pan.shumengya.top/d/scripts/frp/stopkill_frp.sh" | sudo bash
SERVICE_FRPC="smy-frpc"
SERVICE_FRPS="smy-frps"
log() { printf '[frp-停止] %s\n' "$*" >&2; }
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
log "请使用 root 权限运行"
exit 1
fi
stop_service() {
local svc=$1
if systemctl is-active --quiet "$svc"; then
log "正在停止服务 $svc..."
systemctl disable --now "$svc" 2>/dev/null || true
systemctl stop "$svc" 2>/dev/null || true
fi
# 即使服务不活跃,也要尝试 disable防止开机自启
systemctl disable "$svc" 2>/dev/null || true
# 重置失败状态
systemctl reset-failed "$svc" 2>/dev/null || true
}
kill_process() {
local name=$1
local pids
local current_pid=$$
# 尝试精确匹配
pids=$(pgrep -x "$name" || true)
# 如果没找到,尝试模糊匹配但排除当前脚本
if [ -z "$pids" ]; then
pids=$(pgrep -f "$name" | grep -v "$current_pid" || true)
fi
if [ -n "$pids" ]; then
log "发现 $name 残留进程 PID: $pids,正在强制终止..."
kill -9 $pids 2>/dev/null || true
fi
}
log "开始清理 frp 相关服务..."
# Stop Services
stop_service "$SERVICE_FRPC"
stop_service "$SERVICE_FRPS"
# Kill Processes
kill_process "frpc"
kill_process "frps"
log "正在验证服务状态..."
if pgrep -x "frpc" >/dev/null || pgrep -x "frps" >/dev/null; then
log "警告: 仍有 frp 进程在运行!"
ps -fp $(pgrep -x "frpc" "frps" 2>/dev/null) || true
else
log "frp 相关服务及进程已完全停止 (服务已禁用)。"
fi