编写Shell脚本,检测端口是否存在,不存在的用if判断进入 在重启

需要运行的端口包括 8101、8102、8103、8104、8105、8106、8107、8108、8109、8110,这10个端口号

arr1 = [8101、8102、8103、8104、8105、8106、8107、8108、8109、8110];

通过命令获取81在运行的端口

netstat -tlpn | grep 81

好比现在检测出来有8101 、8109、8103在使用

arr2 = [8101 、8109、8103]

然后进行循环判断没运行的81开头端口进行指定输出;好让命令自动重启改服务

#!/bin/bash
a=lsof -i:80 | wc -l
if [ "$a" -gt "0" ];then
echo "0"
else
echo "1"
fi
有例子