qwq

#!/bin/bash

# 定义检测网络的网站
PING_TARGET=www.baidu.com

# 定义todesk的服务重启命令
TODESK_START_CMD="sudo systemctl restart todeskd.service" 

# 定义网络断开时的标志文件位置
FLAG_FILE="/home/sxq/tmp/todesk_down.flag"

# 检测网络断开还是恢复
function check_network() {
    if ping -c 1 $PING_TARGET &> /dev/null; then
        # 网络通了,检查标志文件是否存在
        if [ -e $FLAG_FILE ]; then
            echo "Network is back. Restarting todesk..."
            # 重启todesk服务
            $TODESK_START_CMD
            # 删除标志文件
            rm $FLAG_FILE
        #else
        #    echo "todesk is working..."
        fi
    else
        # 网络断开,创建标志文件
        # echo "Network is down. Waiting for restoration..."
        touch $FLAG_FILE
    fi
}

# 永久循环检测
while true; do
    check_network
    # 检测间隔,按需求调整
    sleep 30
done
posted @ 2024-05-24 21:40  SXQ-BLOG  阅读(14)  评论(0编辑  收藏  举报