#!/bin/bash
# 检查frpc是否正常的脚本，防止网络故障导致的frpc一直没有正常启动
# 定义log文件位置

log_file=/opt/frpc/log

while true
do
  # 检查frpc 的状态是否正常，如果是 "Result: exit-code" 或 "dead"，则重启frp
  current_time=$(date +"%Y-%m-%d %H:%M:%S")
  res_status=$(systemctl status frpc  | grep -i "Active"  | awk -F '[()]' '{print $2}')
  if [[ "$res_status" == *"Result: exit-code"* ]] || [[ "$res_status" == *"dead"* ]]; then
    # 执行 systemctl restart frpc
    systemctl restart frpc
    echo "frpc has been restarted at $current_time." >> $log_file
  else
    echo "No need to restart frpc." >> $log_file
  fi
  sleep 60
done  & disown
