我有一个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的东西),现在-z开关不起作用了:
$ nc -z localhost 8080
nc: invalid option -- 'z'看看CentOS 7中的man nc页面,我看不到任何可以替代-z的明确选择。我应该如何修复我的bash程序以使其在CentOS 7中工作,有什么建议吗?
发布于 2016-06-21 03:35:15
还有一个你真正想要的精简版本:
#!/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发布于 2018-04-01 09:15:50
https://stackoverflow.com/questions/36663738
复制相似问题