一个用于运行 Java SpringBoot 的 Linux 脚本 run.sh
- 自动根据所在目录获取最新的可执行 jar、war
- 自动使用环境变量 JAVA_HOME 进行执行命令(可配置)
- 支持优雅下线(默认等待60秒,可配置,如果超过60秒则强制下线)
- 支持监听 SpringBoot 上线状态
- 支持添加自定义运行参数(如:JVM 参数、SpringBoot 参数)
- 支持常用功能:启动、停止、重启、查看状态、查看日志
- SpringBoot 项目支持查看应用端口信息
目录结构如下
├── fastboot-0.0.1.jar # 应用jar
├── logs
│ ├── console.log # 应用控制台输入的日志
│ └── server.pid # 应用的pid
└── run.sh # 执行脚本
可配置的参数
1. 用法
添加文件的执行权限
# 首次执行第一次即可
chmod +x run.sh
重启并清空日志
查看状态
启动
停止
2. 说明
# 赋予执行权限(初始化执行一次)
chmod +x run.sh
# 启动应用程序
sh run.sh start
# 停止应用程序
sh run.sh stop
# 查看应用程序
# sh run.sh status
# 重启应用程序
sh run.sh restart c # 加 c 删除历史日志文件
# 查看应用日志
sh run.sh logs
3. 源码(run.sh)
#!/bin/bash
#
# ================================================ 说明 start ================================================
# 1. 赋予执行权限: chmod +x run.sh
# 2. 启动应用程序: sh run.sh start
# 3. 停止应用程序: sh run.sh stop
# 4. 查看应用程序: sh run.sh status
# 5. 重启应用程序: sh run.sh restart c # 加 c 删除历史日志文件
# 6. 查看应用日志: sh run.sh logs
# ================================================ 说明 end ================================================
#
# ================================================ 作者 start ================================================
# @author: houyu
# @date: 2021-08-19
# @mail: for.houyu@qq.com(272694308@qq.com)
# @blog: https://www.ihouyu.cn
# @csdn: https://blog.csdn.net/JinglongSource
# ================================================ 作者 end ================================================
# 刷新环境变量
source /etc/profile
# ================================================ 参数 start ================================================
# ------ Java 路径, 可以是 JAVA_HOME 也可以 Java 可执行文件路径, 默认使用环境变量的 JAVA_HOME
#JAVA="/usr/local/java/jdk1.8.0_281/bin/java"
#JAVA="/usr/local/java/jdk1.8.0_281"
JAVA="${JAVA_HOME}"
# ------ 文件路径
DIR_PATH=`cd $(dirname $0); pwd`
# ------ 服务文件名(默认使用目录下最新的.jar或者.war文件)
SERVER_FILE_NAME=`ls -t ${DIR_PATH} | egrep '\.jar|\.war' | head -1`
# ------ Java 参数
JAVA_OPT="${JAVA_OPT} -Duser.timezone=Asia/Shanghai"
JAVA_OPT="${JAVA_OPT} -Xms128m -Xmx128m"
# ------ Spring 参数
SPRING_OPT="${SPRING_OPT} --spring.profiles.active=prod"
# ------ 控制台文件
#CONSOLE_FILE="/dev/null"
CONSOLE_FILE="${DIR_PATH}/logs/console.log"
# ------ 停止时等待超时的秒数
STOP_TIMEOUT=60
#
# ================================================ 参数 end ================================================
#
# ================================================ 方法 start ================================================
#
# 服务PID
_SERVER_PID=0
# 服务端口
_SERVER_PORT=0
# 日志目录
_LOG_PATH="${DIR_PATH}/logs"
# 输入的第二个参数
_P2=$2
#
# =======================================
# 解析服务的 pid
# =======================================
resolve_server_pid() {
_SERVER_PID=0
if [ -f "${_LOG_PATH}/server.pid" ]; then
# pid 文件存在则读取文件
_SERVER_PID=$(cat "${_LOG_PATH}/server.pid")
# 判断是否真的存在这个 pid
if test $(ps -ef | awk '{print $2}' | grep -w ${_SERVER_PID} | wc -l) -eq 0; then
# 没有找到文件记录的 pid, 删除这个文件
rm -rf ${_LOG_PATH}/server.pid
sleep 0.2
_SERVER_PID=0
# 重新解析服务的 pid
#resolve_server_pid
fi
#else
# # pid文件不存在则尝试根据应用程序的名称获取pid
# if test $(pgrep -f ${SERVER_FILE_NAME} | wc -l) -gt 0; then
# # 说明应用程序正在跑, 使用第一个pid
# _SERVER_PID=$(pgrep -f ${SERVER_FILE_NAME} | head -1)
# else
# _SERVER_PID=0
# fi
fi
}
#
# =======================================
# 解析服务的端口
# =======================================
retry_resolve_server_port() {
_SERVER_PORT=0
# 解析服务的 pid
resolve_server_pid
# 检测5分钟(300秒)
for(( i=0; i<=300; i++ )); do
# 判断 pid 是否存在
if test $(ps -ef | awk '{print $2}' | grep -w ${_SERVER_PID} | wc -l) -eq 0; then
rm -rf ${_LOG_PATH}/server.pid
_SERVER_PID=0
break
fi
# 判断 pid 的 port 是否绑定
if test $(netstat -tulnp | grep "${_SERVER_PID}/" | wc -l) -gt 0; then
_SERVER_PORT=$(netstat -tulnp | grep "${_SERVER_PID}/" | head -1 | awk '{print $4}' | awk -F ":" '{print $NF}')
break
fi
[[ $i%5 -eq 0 ]] && echo "--- Observing server port using ${i}s"
sleep 1
done
}
#
# =======================================
# 启动
# =======================================
start() {
echo "----------------------------------------------------------------------------- Start [0] ------"
# 解析服务的 pid
resolve_server_pid
if [ "${_SERVER_PID}" -ne 0 ]; then
# 如果 _SERVER_PID != 0, 那就说明应用程序在运行,不进行启动
echo "--- Do not start, ${SERVER_FILE_NAME} already started! [ SERVER_PID = ${_SERVER_PID} ]"
echo "----------------------------------------------------------------------------- Start [1] ------"
return
fi
#
echo "--- Starting ${SERVER_FILE_NAME} ..."
# 如果 JAVA 是一个目录,则加上/bin/java
[ -d "${JAVA}" ] && JAVA="${JAVA}/bin/java"
# 如果 JAVA 不是一个存在的文件, 则尝试使用默认的java
[ ! -e "$JAVA" ] && JAVA=`which java`
# 最终 Java 不是一个存在的文件, 则清空
[ ! -e "$JAVA" ] && unset JAVA
if [ -z "${JAVA}" ]; then
echo -e "\033[31mPlease set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better!\033[0m"
exit 1
fi
# 判断是否需要删除日志文件
if [[ "$_P2" == "c" ]]; then
# 如果第二个参数是c, 删除日志文件
echo -e "--- \033[33mDeleting ${SERVER_FILE_NAME} log file(${DIR_PATH}/logs/*)\033[0m"
rm -rf ${DIR_PATH}/logs/*
fi
# 检查日志目录
test -d ${DIR_PATH}/logs/ || mkdir -p ${DIR_PATH}/logs/
# 执行脚本
# nohup java -Xmx128m -jar /home/app/server.jar --spring.profiles.active=prod >> /home/app/logs/console.log 2>&1 & echo $! > /home/app/logs/server.pid
#
# 运行的参数
RUN_OPT="${JAVA_OPT} -jar"
RUN_OPT="${RUN_OPT} ${DIR_PATH}/${SERVER_FILE_NAME}"
[ -n "${SPRING_OPT}" ] && RUN_OPT="${RUN_OPT} ${SPRING_OPT}"
#
echo "--- $JAVA ${RUN_OPT}"
echo "$JAVA ${RUN_OPT}" > ${CONSOLE_FILE}
nohup $JAVA ${RUN_OPT} >> ${CONSOLE_FILE} 2>&1 & echo $! > ${_LOG_PATH}/server.pid
#
echo "--- ${SERVER_FILE_NAME} is running"
echo -e "--- You can check the log file \033[36m${CONSOLE_FILE}\033[0m or execute the command on the next line"
echo -e "--- \033[40;37mtail -f -n 300 ${CONSOLE_FILE}\033[0m"
# 睡眠1秒
sleep 1
# 解析服务的 pid
resolve_server_pid
# 判断应用程序是否成功跑起来
if [ "${_SERVER_PID}" -eq 0 ]; then
echo -e "--- \033[31mStart failure ${SERVER_FILE_NAME}\033[0m"
else
if [ -n "${SPRING_OPT}" ]; then
# 如果是 Spring 项目, 需要观察端口是否成功绑定
retry_resolve_server_port
#
if [ "${_SERVER_PORT}" -eq 0 ]; then
echo -e "--- \033[31mStart failure ${SERVER_FILE_NAME}\033[0m"
else
_SERVER_PORT_TEXT=`netstat -tulnp | grep "${_SERVER_PID}/" | awk 'BEGIN{ORS=" "}{print $4}'`
echo -e "--- \033[32mStart successfully ${SERVER_FILE_NAME} [ SERVER_PID = ${_SERVER_PID}, SERVER_PORT = ${_SERVER_PORT_TEXT}]\033[0m"
fi
else
echo -e "--- \033[32mStart successfully ${SERVER_FILE_NAME} [ SERVER_PID = ${_SERVER_PID}, \033[0m\033[33mSERVER_PORT = unknown \033[0m\033[32m]\033[0m"
fi
fi
echo "----------------------------------------------------------------------------- Start [1] ------"
}
#
# =======================================
# 停止
# =======================================
stop() {
echo "----------------------------------------------------------------------------- Stop [0] ------"
# 解析服务的 pid
resolve_server_pid
if [ "${_SERVER_PID}" -eq 0 ]; then
# _SERVER_PID = 0, 那就说明应用程序没有在运行
echo "--- Stopped ${SERVER_FILE_NAME}"
echo "----------------------------------------------------------------------------- Stop [1] ------"
return
fi
echo "--- Graceful Stopping ${SERVER_FILE_NAME} ..."
# 进行优雅停机
kill -15 ${_SERVER_PID}
# 当前等待秒数
_wait_seconds=0
#
while true; do
if test $(ps -ef | awk '{print $2}' | grep -w ${_SERVER_PID} | wc -l) -eq 0; then
echo "--- Stopped ${SERVER_FILE_NAME}"
break
fi
if [ "${_wait_seconds}" -ge "${STOP_TIMEOUT}" ]; then
# 强制终止进程
echo -e "--- \033[31mWait timeout(${_wait_seconds}s), forced shutdown [kill -9 ${SERVER_FILE_NAME}].\033[0m"
sudo kill -9 ${_SERVER_PID}
break
fi
sleep 1
let _wait_seconds++
echo "--- Wait ${_wait_seconds}s"
done
if [ $? -eq 0 ]; then
echo "--- Stop successfully ${SERVER_FILE_NAME}"
rm -rf ${_LOG_PATH}/server.pid
else
echo "--- Stop failure ${SERVER_FILE_NAME}"
fi
echo "----------------------------------------------------------------------------- Stop [1] ------"
}
#
# =======================================
# 状态
# =======================================
status() {
echo "----------------------------------------------------------------------------- status [0] ------"
# 解析服务的 pid
resolve_server_pid
if [ "${_SERVER_PID}" -eq 0 ]; then
echo -e "--- \033[33mStopped $SERVER_FILE_NAME\033[0m"
else
_SERVER_PORT_TEXT=`netstat -tulnp | grep "${_SERVER_PID}/" | awk 'BEGIN{ORS=" "}{print $4}'`
[ -z "$_SERVER_PORT_TEXT" ] && _SERVER_PORT_TEXT="unknown "
echo -e "--- \033[32mRunning ${SERVER_FILE_NAME} [ SERVER_PID = ${_SERVER_PID}, SERVER_PORT = ${_SERVER_PORT_TEXT}]\033[0m"
echo -e "--- You can check the log file \033[36m${CONSOLE_FILE}\033[0m or execute the command on the next line"
echo -e "--- \033[40;37mtail -f -n 300 ${CONSOLE_FILE}\033[0m"
fi
echo "----------------------------------------------------------------------------- status [1] ------"
}
#
# =======================================
# 日志
# =======================================
logs() {
tail -f -n 3000 ${CONSOLE_FILE}
}
#
# ================================================ 方法 end ================================================
#
case "$1" in
'start')
start
;;
'stop')
stop
;;
'restart')
stop
start
;;
'status')
status
;;
'logs')
logs
;;
*)
echo "requires parameter [start|stop|restart|status|logs] [c]?"
exit 1
;;
esac
#
#
#
- 公众号:IT加载中(it_loading)
- CSDN:https://blog.csdn.net/JinglongSource
- 博客:https://ihouyu.cn/
- 邮箱:for.houyu@qq.com