我有一个bash程序,用于检查给定端口中的守护程序是否正常工作:
nc -z localhost $port > /dev/null
if [ "$?" != "0" ]
then
echo The server on port $port is not working
exit
fi
这个程序在CentOS 6中完美运行。但是,似乎CentOS 7改变了nc
命令的底层实现(CentOS 6似乎使用Netcat而CentOS 7使用了另一种叫做Ncat的东西)现在{ {1}}开关不起作用:
-z
查看CentOS 7中的$ nc -z localhost 8080
nc: invalid option -- 'z'
页面,我看不到man nc
的任何明确替代方案。有关如何修复我的bash程序以使其在CentOS 7中运行的任何建议吗?
答案 0 :(得分:2)
以及你真正想要的精简版:
#!/bin/bash
# Start command: nohup ./check_server.sh 2>&1 &
check_server(){ # Start shell function
checkHTTPcode=$(curl -sLf -m 2 -w "%{http_code}\n" "http://10.10.10.10:8080/" -o /dev/null)
if [ $checkHTTPcode -ne 200 ]
then
# Check failed. Do something here and take any corrective measure here if nedded like restarting server
# /sbin/service httpd restart >> /var/log/check_server.log
echo "$(date) Check Failed " >> /var/log/check_server.log
else
# Everything's OK. Lets move on.
echo "$(date) Check OK " >> /var/log/check_server.log
fi
}
while true # infinite check
do
# Call function every 30 seconds
check_server
sleep 30
done
答案 1 :(得分:1)