gongxiangp2/scripts/setup-frpc-auto-restart.sh

101 lines
2.4 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.

#!/bin/bash
# 设置FRP客户端每天凌晨4点自动重启
# 版本: 1.0
# 日期: 2026-01-12
# 服务器: 192.168.58.20 (素材库服务器)
set -e
echo "========================================"
echo "设置FRP客户端自动重启"
echo "========================================"
echo ""
# 检查是否以root运行
if [ "$EUID" -ne 0 ]; then
echo "❌ 请以root用户运行此脚本"
exit 1
fi
# 创建systemd service文件(用于重启frpc)
cat > /etc/systemd/system/frpc-restart.service << 'EOF'
[Unit]
Description=Restart FRP Client Service
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/systemctl restart frpc
StandardOutput=journal
StandardError=journal
EOF
echo "✅ 已创建 frpc-restart.service"
echo ""
# 创建systemd timer文件(每天凌晨4点执行)
cat > /etc/systemd/system/frpc-restart.timer << 'EOF'
[Unit]
Description=Daily restart FRP Client at 4:00 AM
Requires=frpc-restart.service
[Timer]
# 每天凌晨4:00执行
OnCalendar=*-*-* 04:00:00
# 如果错过了执行时间,立即执行(例如服务器在4点后启动)
Persistent=true
# 随机延迟0-300秒,避免多个服务同时重启(客户端和服务端错开)
RandomizedDelaySec=0
[Install]
WantedBy=timers.target
EOF
echo "✅ 已创建 frpc-restart.timer"
echo ""
# 重新加载systemd
systemctl daemon-reload
echo "✅ Systemd已重新加载"
echo ""
# 启用并启动timer
systemctl enable frpc-restart.timer
systemctl start frpc-restart.timer
echo "✅ 已启用并启动定时器"
echo ""
# 显示timer状态
echo "📋 定时器状态:"
systemctl status frpc-restart.timer --no-pager -l || true
echo ""
# 显示下次执行时间
echo "📅 下次执行时间:"
systemctl list-timers frpc-restart.timer --no-pager || true
echo ""
echo "========================================"
echo "设置完成"
echo "========================================"
echo ""
echo "📊 配置说明:"
echo " - 服务名称: frpc-restart.service"
echo " - 定时器: frpc-restart.timer"
echo " - 执行时间: 每天凌晨4:00"
echo " - 随机延迟: 0-300秒(避免同时重启)"
echo ""
echo "📋 管理命令:"
echo " # 查看状态"
echo " systemctl status frpc-restart.timer"
echo " systemctl list-timers frpc-restart.timer"
echo ""
echo " # 手动触发(测试)"
echo " systemctl start frpc-restart.service"
echo ""
echo " # 禁用定时器"
echo " systemctl disable frpc-restart.timer"
echo " systemctl stop frpc-restart.timer"
echo ""