Tomcat服务重启脚本
#此脚本适用于本地安装tomcat环境,操作脚本重启。
[root@ansible_clent ~]# cat restart_tomcat.sh
#!/bin/bash
function shutdown_tomcat () {
#tomcat_id=`jps | grep Bootstrap | awk '{print $1}'`
tomcat_id=`lsof -i:8080 | awk 'NR==2''{print $2}'`
if [ ! $tomcat_id ];then
echo "tomcat process in ${ip} is not exist."
else
echo "shoutdown ${ip} tomcat"
/bin/kill -9 $tomcat_id
fi
}
function start_tomcat () {
echo "start $ip tomcat"
/bin/sh /data/tomcat/bin/catalina.sh start
/usr/local/java/bin/jps
}
function restart_tomcat () {
shutdown_tomcat
start_tomcat
}
restart_tomcat
#此脚本限于在跳板机上使用操作Tomcat重启服务(机器之间做了ssh密钥认证)
[root@ansible_clent ~]# cat restart_tomcat.sh
#!/bin/sh
cat <<EOF
------------------------------------
重启某些ip的tomcat服务,ip地址以,隔开
------------------------------------
EOF
function shutdown_tomcat() {
ip=$1
tomcat_id=`ssh $ip ps -ef | grep tomcat | grep -v grep |awk '{print $2}'`
if [ ! $tomcat_id ];then
echo "tomcat process in ${ip} is not exist."
else
echo "shutdown tomcat in ${ip}"
ssh $ip "/bin/kill -9 $tomcat_id"
fi
}
function start_tomcat(){
ip=$1
echo "start tomcat in ${ip}"
ssh $ip "JAVA_HOME=/usr/local/java/jdk1.7.0_80;export JAVA_HOME;/bin/sh /data/tomcat/bin/startup.sh"
ssh $ip "/usr/local/java/jdk1.7.0_80/bin/jps"
}
read -p "请输入你的ip地址列表[以,隔开]:" pool_ip_list
all_list=`awk 'BEGIN{list="'$pool_ip_list'";split(list,ip_list,",");for(ip in ip_list){print ip_list[ip];}}'`
for ip in ${all_list[*]};do
shutdown_tomcat $ip
start_tomcat $ip
done
Tomcat 日志清理小脚本
#查看磁盘的大小和输出格式:
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 20G 5.8G 13G 31% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/vdb 40G 12G 26G 32% /data
#截取/data目录使用磁盘大小的百分比
$ max=`df -h |awk 'NR==4''{print $5 }'| cut -d% -f1`
$ echo $max
32
#创建清除日志的脚本
cat clean_log.sh
#!/bin/bash
max=`df -h |awk 'NR==4''{print $5 }'| cut -d% -f1`
if [ "$max" -gt 75 ];then
echo " " > /data/tomcat/logs/catalina.out
find /data/tomcat/logs/ -type f -name "*.log" -mtime +5 | xargs rm -rf
find /data/tomcat/logs/ -type f -name "localhost_*.txt" -mtime +7 |xargs rm -rf
fi
#创建计划任务,每1小时运行脚本1次
$ crontab -l
0 * * * * /bin/sh /data/clean_log.sh
更多Tomcat相关教程见以下内容:
CentOS 6.6下安装配置Tomcat环境
RedHat Linux 5.5安装JDK+Tomcat并部署Java项目
Linux下使用Xshell查看Tomcat实时日志中文乱码解决方案