本文是继上一篇 FRP 内网穿透-不做留守嗑盐人 的续文。
上文提出的问题
- 内网服务器断网
- FRPc程序挂了
- 以上两个问题都有可能是因为服务器被重启了
那么下面就是解决办法
Crontab -e 设置定时任务
crontab -e # 进入类vim页面
# 输入如下:意思是每个小时的三十分钟,运行下面两个 shell 脚本
30 * * * * /usr/bin/bash /home/xxx/frp/restart.sh
30 * * * * /usr/bin/bash /home/xxx/frp/checkNetwork.sh
FRPc 检查和重启代码
restart.sh
#!/bin/bash
echo "CHECK FRPC DATE:" $(date +"%Y-%m-%d %H:%M:%S")
ps -ef | grep "./frpc -c ./frpc.ini" | grep -v "grep"
if [ "$?" -eq 0 ]
then
echo "process already started!"
echo -e `date '+%F %T %A'` "\n process already started!" >> CHECK_FRPC_LOG.txt
else
echo "process has been closed"
echo -e `date '+%F %T %A'` "\n process has been closed!" >> CHECK_FRPC_LOG.txt
nohup ./frpc -c ./frpc.ini & #启动应用
echo "process has been restarted"
echo -e `date '+%F %T %A'` "\n process has been restarted" >> CHECK_FRPC_LOG.txt
fi
exit 0
Network 检查和重连代码
checkNetwork.sh
#!/bin/bash
#检测网络链接畅通
function network()
{
#超时时间
local timeout=1
#目标网站
local target=www.baidu.com
#获取响应状态码
local ret_code=`curl -I -s --connect-timeout ${timeout} ${target} -w %{http_code} | tail -n1`
if [ "x$ret_code" = "x200" ]; then
#网络畅通
return 1
else
#网络不畅通
return 0
fi
return 0
}
network
if [ $? -eq 0 ];then
# 网络断了,那么执行下面命令重连
echo "The network is bad"
echo -e `date '+%F %T %A'` "\n The network is bad!" >> CHECK_NETWORK_LOG.txt
curl -s -o /dev/null -d "DDDDD=账号&upass=密码&0MKKey=" https://drcom.szu.edu.cn
network
if [ $? -eq 1 ];then
echo "The network has been reconnected!"
echo -e `date '+%F %T %A'` "\n The network has been reconnected!" >> CHECK_NETWORK_LOG.txt
else
echo "Unable to connect network"
echo -e `date '+%F %T %A'` "\n Unable to connect network!" >> CHECK_NETWORK_LOG.txt
fi
exit 0
fi
echo "The network is fine"
echo -e `date '+%F %T %A'` "\n The network is fine!" >> CHECK_NETWORK_LOG.txt
exit 0
版权声明:本文为leaf_130原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。