#!/bin/bash
cd /opt/home/admin
#规避老毛子各种挂死问题

#负载大于50重启
{
load=0
while [ $load -le 50 ]
do
    sleep 600
    load=`uptime | awk -F 'load average: ' '{print $2}' | awk -F. '{print $1}'`
done
mtd_write -r unlock mtd1
}&

#网络不通重启
{
ping="x"
while [ ! "$ping"x = x ]
do
    sleep 600
    ping=`ping -c 2 -W 1 www.baidu.com | grep seq | tr -d '\n'`
done
mtd_write -r unlock mtd1
}&

#SSH不能连接重启
{
ssh="x"
while [ ! "$ssh"x = x ]
do
    sleep 1200
    ssh=`seq 3 | xargs -I {} curl -v -m 20 --output - ftp://vm2.zengge99.ml:9022 2>&1 | grep -a SSH`
done
mtd_write -r unlock mtd1
}&

#内存不足20M重启
{
sleep 600
m=300000
while [ $m -ge 20000 ]
do
    sleep 1
    m=`free | tail -n +2 | head -n 1 | awk '{print $4}'`
done
mtd_write -r unlock mtd1
}&

#如果在测速，杀掉自己，避免误重启
{
proc=`ps | grep cf.sh | grep "\-k" | grep -v grep`
while [ "$proc"x = x ];
do
    sleep 10;
    proc=`ps | grep cf.sh | grep "\-k" | grep -v grep`
done
killall cfdog.sh &
}&


