linux编写脚本,用来监控程序是否掉线

慈云数据 2024-03-25 技术支持 67 0

新建一个文件,并给予权限

linux编写脚本,用来监控程序是否掉线
(图片来源网络,侵删)
touch port_monitor.sh
chmod 777 port_monitor.sh

写入如下命令

#!/bin/bash
# 定义要监控的端口号
PORT=3306
while true; do
    # 使用netstat命令检查端口是否存在
    netstat -tuln | grep ":$PORT "
    if [ $? -ne 0 ]; then
        echo "端口 $PORT 不存在,执行命令..."
 
    else
        echo "端口 $PORT 存在"
    fi
    # 等待1分钟
    sleep 60
done

开启后台启动即可:

linux编写脚本,用来监控程序是否掉线
(图片来源网络,侵删)
./port_monitor.sh &

注意:使用Windows编写的文件是dos格式,需要转换成unix格式:

可以使用vim编辑器进行转换

set fileformat=unix
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon