Tomcat 启动脚本

Tomcat 启动脚本

start.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
#!/usr/bin/env bash

# @Author: YL
# @Date: 2017-01-25 14:50:43
# @Last Modified by: YL
# @Last Modified time: 2017-12-18 14:12:23

cd `dirname $0`
PROJECT_HOME=`pwd`

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

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

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