- 前言
- Shell脚本内容
- 给脚本执行权限
- 控制台彩色输出
- 参考链接
在Linux环境通过jar包启动Java进程手动输命令有点麻烦,参数多容易忘记,编写一个能启动停止的脚本会方便很多。下文提供了一个较为通用的脚本,可以灵活指定JVM启动选项和应用参数,能够应付大部分场景,有后台启动、前台启动、停止、重启、查看进程状态功能。
Shell脚本内容脚本用java、jps相关指令实现功能,echo彩色输出到控制台。
要注意的是后台启动时丢弃掉了输出到控制台的日志(> /dev/null),也就是启动时不会输出进程启动打印的日志,因为一般项目内会有配置输出到指定的日志文件(如:Logback),可以通过这些日志文件查看服务启动和运行的状态。
# !/bin/bash
#===========================================================================================
# APP_HOME=/opt/soft/app 工作目录,存放jar包和日志等相关文件
# APP_JAR=app.jar jar包全称
# JVM_OPTS="-Xms256m -Xmx256m" JVM启动参数
# APP_OPTS="--server.port=8080 --spring.profiles.active=dev" spring配置参数、main方法接收的参数
#===========================================================================================
APP_HOME=/opt/soft/
APP_JAR=app.jar
APP_OPTS=""
JVM_OPTS="-Xms256m -Xmx256m"
function start(){
findPid
if [ -z "$pid" ]; then
java -jar $JVM_OPTS $APP_HOME/$APP_JAR $APP_OPTS > /dev/null &
colorPrint $GREEN "$APP_JAR 已启动"
else
colorPrint $YELLOW "$APP_JAR 启动失败,已存在运行的进程,进程ID为 $pid"
exit 1
fi
}
function startForeground(){
findPid
if [ -z "$pid" ]; then
colorPrint $GREEN "$APP_JAR 开始启动"
java -jar $JVM_OPTS $APP_HOME/$APP_JAR $APP_OPTS
else
colorPrint $YELLOW "$APP_JAR 启动失败,已存在运行的进程,进程ID为 $pid"
exit 1
fi
}
function stop(){
findPid
if [ -z "$pid" ]; then
colorPrint $YELLOW "$APP_JAR 停止失败,未找到运行的进程"
else
kill -TERM $pid
colorPrint $GREEN "$APP_JAR 已停止ID为 $pid 的进程"
fi
}
function restart(){
stop
sleep 3
start
}
function status() {
info=$(jps -mlvV | grep $APP_JAR)
if [ -z "$info" ]; then
colorPrint $YELLOW "$APP_JAR 未找到运行的进程"
else
echo $info
fi
}
# 根据APP_JAR查找进程ID
pid=
function findPid(){
pid=$(jps | grep $APP_JAR | awk '{print $1}')
}
# 按指定的颜色打印字符串。第一个参数是颜色,第二个参数是打印的字符串
RED=31 GREEN=32 YELLOW=33
function colorPrint(){
echo -e " 33[$1m $2 33[0m"
}
case "$1" in
start)
start
;;
start-foreground)
startForeground
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
colorPrint $RED "Usage: {start | start-foreground | stop| restart | status} {后台启动|前台启动|停止|重启|查看进程状态}"
exit 1
;;
esac
如果需要指定日志文件可以将下面这段替换掉脚本中的start()函数:
function start(){
findPid
if [ -z "$pid" ]; then
nohub java -jar $JVM_OPTS $APP_HOME/$APP_JAR $APP_OPTS >> $APP_HOME/nohub.out 2>&1 &
colorPrint $GREEN "$APP_JAR 已启动"
else
colorPrint $YELLOW "$APP_JAR 启动失败,已存在运行的进程,进程ID为 $pid"
exit 1
fi
}
给脚本执行权限
# 格式为chmod +x <文件名> chmod +x server.sh控制台彩色输出
使用echo在控制台输出字符的时候可以指定背景和字体颜色。
- 指定字体颜色,颜色值范围30~37
# echo -e " 33[<字体颜色值>m<要输出的字符> 33[0m" echo -e " 33[30m黑色字 33[0m" echo -e " 33[31m红色字 33[0m" echo -e " 33[32m绿色字 33[0m" echo -e " 33[33m黄色字 33[0m" echo -e " 33[34m蓝色字 33[0m" echo -e " 33[35m紫色字 33[0m" echo -e " 33[36m天蓝字 33[0m" echo -e " 33[37m白色字 33[0m"
- 指定字体色的同时还可以指定背景色,背景颜色值的范围为40~47,字体颜色和背景颜色可以随意搭配
# echo -e " 33[<背景颜色值>;<字体颜色值>m<打印的字符串> 33[0m" echo -e " 33[40;37m黑底白字 33[0m" echo -e " 33[41;37m红底白字 33[0m" echo -e " 33[42;37m绿底白字 33[0m" echo -e " 33[43;37m黄底白字 33[0m" echo -e " 33[44;37m蓝底白字 33[0m" echo -e " 33[45;37m紫底白字 33[0m" echo -e " 33[46;37m天蓝底白字 33[0m" echo -e " 33[47;30m白底黑字 33[0m"参考链接
kill 命令信号



