touch tailscaled_monitor.sh
#!/bin/bash
# tailscaled 进程监控脚本
# 功能:检测 tailscaled 进程是否存活,如果死掉则自动重启
LOG_FILE="/var/log/tailscaled_monitor.log"
CHECK_INTERVAL=10 # 检测间隔(秒)
# 日志函数
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# 启动 tailscaled 函数
start_tailscaled() {
log_message "检测到 tailscaled 进程已停止,正在重启..."
tailscaled \
--state=/var/lib/tailscale/tailscaled.state \
--socket=/var/run/tailscale/tailscaled.sock \
--tun=userspace-networking &
sleep 2
if pgrep -f "tailscaled.*--state=/var/lib/tailscale/tailscaled.state" > /dev/null; then
log_message "tailscaled 进程重启成功 (PID: $(pgrep -f 'tailscaled.*--state=/var/lib/tailscale/tailscaled.state'))"
else
log_message "错误:tailscaled 进程重启失败"
fi
}
# 检查 tailscaled 进程
check_tailscaled() {
# 使用 pgrep 查找包含特定参数的 tailscaled 进程
if pgrep -f "tailscaled.*--state=/var/lib/tailscale/tailscaled.state" > /dev/null; then
return 0 # 进程存在
else
return 1 # 进程不存在
fi
}
log_message "tailscaled 监控脚本启动"
# 主循环
while true; do
if ! check_tailscaled; then
start_tailscaled
fi
sleep "$CHECK_INTERVAL"
done
chmod +x tailscaled_monitor.sh
nohup ./tailscaled_monitor.sh > /dev/null 2>&1 &
tail -f /var/log/tailscaled_monitor.log
Top comments (0)