---
title: "Linux监听Tomcat"
author: "xuxujing"
date: "2014-08-12T15:22:29Z"
tags: []
categories: ["行业应用"]
slug: "linux-tomcat"
draft: false
lastmod: "2014-08-12T15:22:29Z"
id: 2102895
url: "https://xujava.iteye.com/blog/2102895"
---

1.编写如下脚本，不需要多次判断的去掉循环

 

```
#!/bin/bash
count=4
seconds=2
echo $(date '+%Y-%m-%d %H:%M:%S')" Test Start"
for i in $(seq $count)
do
	webs=$(/usr/bin/wget -S --spider --tries=3 --timeout=3 http://localhost:8080/test/status.html 2>&1 | grep HTTP/1.1|awk '{print $2}')
	if [ "$webs" = "200" ]
	then
		echo $(date '+%Y-%m-%d %H:%M:%S')" Test success"
		break;
	else
		echo $(date '+%Y-%m-%d %H:%M:%S')" Test failed "$i
		if [ $i -eq $count ]
		then
			pid=$(ps -ef | grep tomcat |grep -w '/opt/tomcat' | grep -v grep |awk '{print $2}')
			if [ "$pid" != "" ]
			then
				kill -9 $pid
				echo "killed $pid"
				echo $(/opt/tomcat/bin/startup.sh)
			else
				echo "pid is null"
			fi
		else
			sleep $[$i*$seconds]s		
		fi
	fi
done
echo $(date '+%Y-%m-%d %H:%M:%S')" Test End"
echo ""
```

 

2.crontab -e添加定时任务

\*/5 * * * * /xxx/xxx/xx.sh &gt;&gt;/xxx/xxx/xx.log