一个linux下监听某个端口并拉起任务的shell脚本

  创建监控脚本

#!/bin/bash  
# 定义要监控的端口号  
PORT=8011  

while true; do  
    # 使用netstat或ss命令检查端口是否存在  
    if ! ss -tuln | grep -q ":$PORT"; then  
        echo "$(date) 端口 $PORT 不存在, 正在尝试重启服务..." >> ./port_monitor.log  
        # 重启服务  
        bash /path/to/start.sh  
    else  
        echo "$(date) 端口 $PORT 存在" >> ./port_monitor.log  
    fi  
    # 等待一段时间再次检查  
    sleep 60  
done

 

注意替换脚本和日志为自己的  绝对路径的文件

赋予脚本执行权限

chmod +x your_monitor_script.sh

your_monitor_script.sh替换为你创建的监控脚本的文件名。

后台运行脚本

nohup ./your_monitor_script.sh &

 


posted @ 2024-09-13 23:13  lytcreate  阅读(347)  评论(0)    收藏  举报