shell 脚本启动

# !/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= jar地址
APP_JAR= jar名字
APP_OPTS="--spring.profiles.active=dev"  
JVM_OPTS="-Xms512m -Xmx512m"  
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"
    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 "\033[$1m $2 \033[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

运行之前记得检查java环境是否存在


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值