Files
linux-bash/frp/start_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 服务
# 该脚本自动检测已安装的 frp 服务 (客户端/服务端) 并恢复其运行和开机自启
# 用法curl -fsSL "https://pan.shumengya.top/d/scripts/frp/start_frp.sh" | sudo bash
SERVICE_FRPC="smy-frpc"
SERVICE_FRPS="smy-frps"
log() { printf '[frp-启动] %s\n' "$*" >&2; }
fail() { log "错误: $*" >&2; exit 1; }
# 检查 root 权限
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
fail "请使用 root 权限运行"
fi
start_service() {
local svc=$1
local service_file="/etc/systemd/system/${svc}.service"
if [ -f "$service_file" ]; then
log "检测到已安装服务: $svc"
# 重置失败状态
systemctl reset-failed "$svc" 2>/dev/null || true
log "正在启用并启动 $svc..."
systemctl enable --now "$svc"
sleep 1
if systemctl is-active --quiet "$svc"; then
log "$svc 启动成功 (状态: $(systemctl is-active "$svc"))"
else
log "$svc 启动失败"
systemctl status "$svc" --no-pager -n 5 || true
fi
else
# 仅在调试时显示,避免干扰普通用户
# log "未检测到 $svc跳过"
:
fi
}
log "正在扫描并恢复 frp 服务..."
found_any=false
if [ -f "/etc/systemd/system/${SERVICE_FRPC}.service" ]; then
start_service "$SERVICE_FRPC"
found_any=true
fi
if [ -f "/etc/systemd/system/${SERVICE_FRPS}.service" ]; then
start_service "$SERVICE_FRPS"
found_any=true
fi
if [ "$found_any" = false ]; then
log "未检测到任何已安装的 frp 服务 (smy-frpc 或 smy-frps)。"
log "请先运行安装脚本进行安装。"
exit 1
fi
log "操作完成。"