3月 15

linux定时重启单台上多个tomcat

tomcat长期运行会越来越慢,所以需要定时重启单台上多个tomcat,写一个脚本放在crontab定时运行。脚本如下,需要注意脚本中使用了了linux中的mail命令,在tomcat启动失败时可以发送提醒邮件。(不知道为什么贴到这里格式会乱,只能手动整理)

#!/bin/sh
# ——————————————————————————-
# Filename: tomcat_reboot.sh
# Revision: 1.0
# Date: 2010-03-01
# Author: simon
# Email: simon-zzm@163.com
# Description:
# ——————————————————————————-
# Source function library.
. /etc/init.d/functions
source /etc/profile

#######set tomcat patch
tomcat_path=(“/program/tomcat_a/” “/program/tomcat_b/”)

#######set tomcat_key patch
tom_key_path=(“tomcat_a” “tomcat_b”)

###### set bases data
admin_mail=simon-zzm@163.com
IP=`/sbin/ifconfig |grep ‘inet addr:’|grep -v ‘127.0.0.1’|awk ‘ ”{print $2}’`

###### send alert mail
send_mail()
{
echo ${IP}” “${1}” can not stop…” >${2}tom_mail.log
/bin/mail -s ${1}’-‘${IP} ${admin_mail}<${2}tom_mail.log
/bin/rm -rf ${2}tom_mail.log
}

###### delete tomcat`s work dir and startup server
del_start()
{
/bin/rm -rf ${1}work/*
$1bin/startup.sh
}
######main()
for (( i=0 ; i < ${#tomcat_path[@]} ; i++ ))
do
${tomcat_path[i]}bin/shutdown.sh
sleep 3
get_pid_count=`/bin/ps -ef|grep -v grep|grep ${tom_key_path[i]}|wc -l`
if [ ${get_pid_count} -gt 0 ] ; then
/bin/ps -ef|grep -v grep|grep ${tom_key_path[i]}|awk ‘ ”{print $2}’|xargs kill -9
sleep 5
get_pid_count=`/bin/ps -ef|grep -v grep|grep ${tom_key_path[i]}|wc -l`
if [ ${get_pid_count} -gt 0 ] ; then
send_mail ${tom_key_path[i]} ${tomcat_path[i]}
else
del_start ${tomcat_path[i]}
fi
else
del_start ${tomcat_path[i]}
fi
sleep 3
get_pid_count=`/bin/ps -ef|grep -v grep|grep ${tom_key_path[i]}|wc -l`
if [ ${get_pid_count} -lt 1 ] ; then
send_mail ${tom_key_path[i]} ${tomcat_path[i]}
fi
done