使用 linux shell脚本启动jar包,进行关闭,查看状态

本文介绍了如何使用Shell脚本来启动、停止和检查Java应用程序的状态。首先,通过命令行直接启动jar包,然后展示了一个简单的Shell脚本,用于实现同样的功能。接着,升级脚本,添加了参数支持,可以进行启动、停止和查看状态操作。通过赋予脚本执行权限并执行,可以方便地管理Java应用。
摘要由CSDN通过智能技术生成

一  启动jar包普通方式

1.将jar包打包好,上传服务器,并使用jar包的配置文件:application-dev.yml放到和jar包同一目录下,如下图所示

2. 启动命令:

nohup java -jar -Dfile.encoding=utf-8  -Dspring.config.location=/root/ljf-tmp/application-dev.yml   pmsm-bloc-portal.jar  > nohup.out 2>&1 & 

二 使用shell脚本进行启动jar包

1.编写脚本内容:

#!/bin/bash
source /etc/profile
currTime=`date +"%Y-%m-%d %H:%M:%S"`
APP_NAME=pmsm-bloc-portal.jar
APP_DIR=/root/ljf-tmp
APP_CONFIG_FILE=application-dev.yml
#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
  #如果不存在返回1,存在返回0     
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}
#启动方法
start(){
  is_exist
  if [ $? -eq 0 ]; then
    echo "【 ${APP_NAME} 】is already running. pid=${pid}"
  else
    nohup java -jar -Dfile.encoding=utf-8  -Dspring.config.location=${APP_DIR}/${APP_CONFIG_FILE}   ${APP_DIR}/${APP_NAME}  > nohup.out 2>&1 & 
    pid=`ps -ef|grep java|grep $APP_NAME|awk '{print $2}'`
    echo "${currTime}:start 【 $APP_NAME 】is successfully!!!,this app pid:${pid}"
  fi
}


#程序主入口
start

2.赋予脚本执行权限

[root@km-003 ljf-tmp]# chmod 777 startup-jar.sh
3.执行脚本,启动jar包

[root@km-003 ljf-tmp]# ./startup-jar.sh 
2022-10-24 22:02:37:start pmsm-bloc-portal.jar is successfully!!!,this app pid:2818983


三  使用shell脚本,进行传参启动,查看,停止

1.编写脚本内容

#!/bin/bash
source /etc/profile
#自定义配置
currTime=`date +"%Y-%m-%d %H:%M:%S"`
APP_NAME=pmsm-bloc-portal.jar
APP_DIR=/root/ljf-tmp
APP_CONFIG_FILE=application-dev.yml
#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
  #如果不存在返回1,存在返回0     
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}
#启动方法
start(){
  is_exist
  if [ $? -eq 0 ]; then
    echo "${APP_NAME} is already running. pid=${pid}"
  else
    nohup java -jar -Dfile.encoding=utf-8  -Dspring.config.location=${APP_DIR}/${APP_CONFIG_FILE}   ${APP_DIR}/${APP_NAME}  > nohup.out 2>&1 & 
    pid=`ps -ef|grep java|grep $APP_NAME|awk '{print $2}'`
    echo "${currTime}:start $APP_NAME is successfully!!!,this app pid:${pid}"
  fi
}

#停止方法
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
    echo "${APP_NAME} is already shutdown"
  else
    echo "${APP_NAME} is not running"
  fi  
}

#输出运行状态
status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. Pid is ${pid}"
  else
    echo "${APP_NAME} is NOT running."
  fi
}
#重启
restart(){
  stop
  sleep 5
  start
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    restart
    ;;
  *)
    usage
    ;;
esac

2.赋予脚本执行权限

[root@km-003 ljf-tmp]# chmod 777 startup-jar.sh
3.执行脚本,启动jar包

脚本文件见:

 

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值