[root@ob1 scripts]# vim test_httpd.sh 1 #!/bin/bash 2 #ss -tlnup|grep :80 >/dev/null 2>&1 3 dk=$(ss -tlnup|awk -F"[ :]+" '/80/{print $5}') 4 if [ "80" = "${dk}" ];then #use string to compare,num is not have null 5 echo "httpd is running" 6 else 7 echo "restarting" 8 /etc/init.d/httpd restart >/dev/null 2>&1 9 fi
如果服务没起就重启,if [ "80" = "${dk}" ];then 在字符串比较80 -eq dk 没起是空的时候报错,写成字符串就不报错应为字符串有空,数字没值会报错