从bash shell监视一个自定义的Java HTTP服务器?

我怎么能从一个bash shell监视一个服务端口?

我想要监视一个Java服务(在端口9090上每分钟一次),然后调用“/etc/init.d/myservice -restart”,如果这个服务没有用一个简单的HTML消息来响应的话。

你会怎么做这样的事情?

我的想法是使用类似的东西:

wget -O - --no-check-certificate --progress=dot https://localhost:9090 

要么

 curl --insecure https://localhost:9090 

使用monit ,比编写一个shell脚本更安全。

如果你真的想编写一个脚本,使用curl来获取内容,grep它,并在失败的情况下重新启动服务。

感谢“coredump”。 你给我提示我需要解决这个问题。 我将使用CURL的组合以及本页上的答案。

这是我想出来的,这似乎为我工作。 如果你能改善这个答案,我可能会给你奖励点数。

 #!/bin/bash rm -f listening.htm curl -s --connect-timeout 10 --insecure $1 > listening.htm RETVAL=$? echo "Curl return value : $RETVAL" if [ $RETVAL -eq 2 ]; then echo "Missing URL parameter. Add URL and try again." fi if [ $RETVAL -eq 6 ]; then echo "Unable to resolve host. Check URL and try again." fi if [ $RETVAL -eq 0 ]; then echo "Is the site listening...?" elif [ $RETVAL -eq 7 ]; then echo "Server timeout." elif [ $RETVAL -eq 22 ];then echo "HTTP error above 400" else rm listening.htm exit 1 fi if grep -Fq "The site is listening..." listening.htm then echo "Health is ok." else echo "Service didn't respond. Stopping." /home/ec2-user/SITE/stopService.sh sleep 6 echo "Starting service." /home/ec2-user/SITE/startService.sh sleep 10 echo "Restarted at `date`" >> monitor.log rm listening.htm fi exit 1