Tomcat 启动脚本

Tomcat 启动脚本

主 Tomcat + 应用部署 Tomcat

应用部署方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[txrd@host-148 /data/]$ tree -af 
.
├── ./apache-tomcat-8.5.57
│ └── ./bin
│ └── ./conf
│ └── ./lib
│ └── ./temp
│ └── ./webapps
│ └── ./work
├── ./szim
│ └── ./project
│ └── ./szim.war
│ └── ./tomcat
│ └── ./conf
│ └── ./temp
│ └── ./webapps
│ └── ./work
│ └── ./logs.sh
│ └── ./restart.sh

restart.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash

# @Author: YL
# @Date: 2020-08-17 09:33:43
# @Last Modified by: YL
# @Last Modified time: 2020-08-17 14:50:43

export LANG=en_US.UTF-8

# Tomcat 主目录
export CATALINA_HOME=/data/apache-tomcat-8.5.57

cd `dirname $0`
PROJECT_HOME=`pwd`

# Tomcat 应用部署目录
TC_HOME="$PROJECT_HOME/tomcat"

# Tomcat 应用部署目录
export CATALINA_BASE=$TC_HOME

# crontab 检测日记文件
TC_STDOUT_FILE="$TC_HOME/logs/stdout.out"
# 检测 Tomcat 是否启动(进程ID)
PIDS=`ps -ef | grep $TC_HOME | grep -v grep | awk '{print $2}'`

# 执行检测的用户IP地址
WHO_AM_I=`who am i | awk '{print $5}' | sed 's/(//g' | sed 's/)//g'`

#===========================================================================================
# JVM Configuration
#===========================================================================================
# export JAVA_HOME=/usr/java/jdk1.8.0_251
# export PATH=$JAVA_HOME/bin:$PATH
# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$CLASSPATH
JVM_OPTS="$JVM_OPTS -server -Xms1g -Xmx1g -Xss256k"
JVM_OPTS="$JVM_OPTS -XX:+DisableExplicitGC -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+ParallelRefProcEnabled"
JVM_OPTS="$JVM_OPTS -Xloggc:$TC_HOME/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
JVM_OPTS="$JVM_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$TC_HOME/logs/dump-`date +"%Y-%m-%d"`.hprof"
JVM_OPTS="$JVM_OPTS -Dspring.profiles.active=pro"

# Tomcat 启动参数配置
export JAVA_OPTS="$JVM_OPTS"

#===========================================================================================
# log file delete
#===========================================================================================
function cleanLog(){
ls -d -t $TC_HOME/logs/catalina.*.log | tail -n +3 | xargs rm -rf
ls -d -t $TC_HOME/logs/host-manager.*.log | tail -n +3 | xargs rm -rf
ls -d -t $TC_HOME/logs/localhost.*.log | tail -n +3 | xargs rm -rf
ls -d -t $TC_HOME/logs/manager.*.log | tail -n +3 | xargs rm -rf
ls -d -t $TC_HOME/logs/*.hprof | tail -n +3 | xargs rm -rf
}
cleanLog

# 优雅停机
function gracefulShutdown(){
if [ -z "$PIDS" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME does not started!" | tee -a $STDOUT_FILE
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME kill $PIDS begining" | tee -a $STDOUT_FILE
for PID in $PIDS ; do
kill $PID > /dev/null 2>&1
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME kill $PID success" | tee -a $STDOUT_FILE
done

# 检测是否停机成功
COUNT=0
while [ $COUNT -lt 1 ]; do
sleep 1
COUNT=1
for PID in $PIDS ; do
PID_EXIST=`ps -f -p $PID | grep java`
if [ -n "$PID_EXIST" ]; then
COUNT=0
break
fi
done
done
fi
}

function operate(){
if [[ "$1" = "kill" ]] ; then
gracefulShutdown
elif [[ "$1" = "start" ]]; then
cd $CATALINA_HOME/bin
sh startup.sh
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME is not support $1" | tee -a $TC_STDOUT_FILE
fi
}
if [[ "$1" = "" || "$1" = "restart" || "$1" = "start" ]] ; then
operate kill
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME starting" | tee -a $TC_STDOUT_FILE
operate start
elif [[ "$1" = "kill" ]] ; then
operate kill
elif [[ "$1" = "check" ]] ; then
if [[ "$PIDS" = "" ]] ; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME starting" >> $TC_STDOUT_FILE
operate start
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME pid $PIDS" >> $TC_STDOUT_FILE
fi
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME is not support $1" | tee -a $TC_STDOUT_FILE
fi

单应用部署 Tomcat

应用部署方式

1
2
3
4
5
6
7
8
9
10
11
12
13
[txrd@host-148 /data/szim]$ tree -af 
.
├── ./project
│ └── ./szim.war
└── ./tomcat
│ └── ./bin
│ └── ./conf
│ └── ./lib
│ └── ./temp
│ └── ./webapps
│ └── ./work
├── ./logs.sh
├── ./restart.sh

restart.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash

# @Author: YL
# @Date: 2020-08-17 09:33:43
# @Last Modified by: YL
# @Last Modified time: 2020-08-17 14:50:43

export LANG=en_US.UTF-8

# Tomcat 主目录
export CATALINA_HOME=/data/apache-tomcat-8.5.57

cd `dirname $0`
PROJECT_HOME=`pwd`

# Tomcat 应用部署目录
TC_HOME="$PROJECT_HOME/tomcat"

# Tomcat 应用部署目录
export CATALINA_BASE=$TC_HOME

# crontab 检测日记文件
TC_STDOUT_FILE="$TC_HOME/logs/stdout.out"
# 检测 Tomcat 是否启动(进程ID)
PIDS=`ps -ef | grep $TC_HOME | grep -v grep | awk '{print $2}'`

# 执行检测的用户IP地址
WHO_AM_I=`who am i | awk '{print $5}' | sed 's/(//g' | sed 's/)//g'`

#===========================================================================================
# JVM Configuration
#===========================================================================================
# export JAVA_HOME=/usr/java/jdk1.8.0_251
# export PATH=$JAVA_HOME/bin:$PATH
# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$CLASSPATH
JVM_OPTS="$JVM_OPTS -server -Xms1g -Xmx1g -Xss256k"
JVM_OPTS="$JVM_OPTS -XX:+DisableExplicitGC -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:+ParallelRefProcEnabled"
JVM_OPTS="$JVM_OPTS -Xloggc:$TC_HOME/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M"
JVM_OPTS="$JVM_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$TC_HOME/logs/dump-`date +"%Y-%m-%d"`.hprof"
JVM_OPTS="$JVM_OPTS -Dspring.profiles.active=pro"

# Tomcat 启动参数配置
export JAVA_OPTS="$JVM_OPTS"

#===========================================================================================
# log file delete
#===========================================================================================
function cleanLog(){
ls -d -t $TC_HOME/logs/catalina.*.log | tail -n +3 | xargs rm -rf
ls -d -t $TC_HOME/logs/host-manager.*.log | tail -n +3 | xargs rm -rf
ls -d -t $TC_HOME/logs/localhost.*.log | tail -n +3 | xargs rm -rf
ls -d -t $TC_HOME/logs/manager.*.log | tail -n +3 | xargs rm -rf
ls -d -t $TC_HOME/logs/*.hprof | tail -n +3 | xargs rm -rf
}
cleanLog

# 优雅停机
function gracefulShutdown(){
if [ -z "$PIDS" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME does not started!" | tee -a $STDOUT_FILE
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME kill $PIDS begining" | tee -a $STDOUT_FILE
for PID in $PIDS ; do
kill $PID > /dev/null 2>&1
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME kill $PID success" | tee -a $STDOUT_FILE
done

# 检测是否停机成功
COUNT=0
while [ $COUNT -lt 1 ]; do
sleep 1
COUNT=1
for PID in $PIDS ; do
PID_EXIST=`ps -f -p $PID | grep java`
if [ -n "$PID_EXIST" ]; then
COUNT=0
break
fi
done
done
fi
}

function operate(){
if [[ "$1" = "kill" ]] ; then
gracefulShutdown
elif [[ "$1" = "start" ]]; then
cd $CATALINA_HOME/bin
sh startup.sh
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME is not support $1" | tee -a $TC_STDOUT_FILE
fi
}
if [[ "$1" = "" || "$1" = "restart" || "$1" = "start" ]] ; then
operate kill
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME starting" | tee -a $TC_STDOUT_FILE
operate start
elif [[ "$1" = "kill" ]] ; then
operate kill
elif [[ "$1" = "check" ]] ; then
if [[ "$PIDS" = "" ]] ; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME starting" >> $TC_STDOUT_FILE
operate start
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME pid $PIDS" >> $TC_STDOUT_FILE
fi
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')]-[$1]-[$WHO_AM_I] $TC_HOME is not support $1" | tee -a $TC_STDOUT_FILE
fi

使用说明

1
2
3
4
5
sh restart.sh start # 重启
sh restart.sh # 重启
sh restart.sh restart # 重启
sh restart.sh check # 检测服务是否启动,没有启动,则启动之,否则不做其他操作
sh restart.sh kill # kill掉已启动的服务进程
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/e22f47ab.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!