首页 > 系统相关 >linux-启停jar脚本

linux-启停jar脚本

时间:2022-08-25 10:34:47浏览次数:53  
标签:grep jar pid server 启停 linux echo JARNAME

#!/bin/bash

JAVA_OPTIONS="-Xmx1024m -Xms126m -Duser.timezone=GMT+08"
JARPATH=`pwd`
declare -a JARNAME

function findJAR()
{
  JARNAME=($(ls -l $JARPATH | grep jar$ | awk '{print $NF}'))
  if [[ ${#JARNAME[@]} > 1 ]]
  then
    echo "[${JARNAME[@]}] too many jar files!!! I don't know startup which one."
    exit 1
  elif [[ ${#JARNAME[@]} == 0 ]]
  then
    echo "this is no jar file in $JARPATH"
    exit 1
  else
    echo "---------"
  fi
}

function rename()
{
  if [[ ${#JARNAME[@]} != 1 ]]
  then
    echo "[${JARNAME[@]}] too many jar files!!! I don't know which one should be renamed."
    exit 1
  else
    timestamp=`date "+%Y%m%d%H%M%S"`
    mv "${JARNAME[0]}" "${JARNAME[0]}${timestamp}"
    echo "${JARNAME[0]} has been renamed to ${JARNAME[0]}${timestamp}"
  fi
}

function start()
{
  server_pid=`ps -ef | grep java | grep ${JARNAME[0]} | awk '{print $2}'`
  if [ "${server_pid}" != "" ]
  then
     echo -e "${JARNAME[0]} is alived with pid ${server_pid}.\nabort start action"
     exit 1
  fi
  nohup java ${JAVA_OPTIONS} -jar ${JARNAME[0]} > /dev/null 2>&1 &
  server_pid=`ps -ef | grep java | grep ${JARNAME[0]} | awk '{print $2}'`
  echo "${JARNAME[0]} is alived with pid ${server_pid}."
}

function status()
{
  server_pid=`ps -ef | grep java | grep ${JARNAME[0]} | awk '{print $2}'`
  if [ "${server_pid}" != "" ]
  then
     echo -e "${JARNAME[0]} is alived with pid ${server_pid}."
  else
     echo "${JARNAME[0]} isn't alived."
  fi
}

function stop()
{
  server_pid=`ps -ef | grep java | grep ${JARNAME[0]} | awk '{print $2}'`
  if [ "$server_pid" != "" ]
  then
     echo "${JARNAME[0]} is alived with pid ${server_pid}."
     kill -9 $server_pid
     echo "has stopped ${JARNAME[0]}"
  else
     echo "${JARNAME[0]} isn't alived."
  fi
}

function usage(){
   echo -e "Usage:sh deploy.sh COMMAND \nCOMMAND OPTIONS:\nstart:start the server\nstop:stop the server\nrestart:restart the server\nstatus:show the server pid.\nrename:rename the jar with timestamp.\n"
}

findJAR

case "$1" in
   "start")
     start
   ;;
   "stop")
     stop
   ;;
   "restart")
     stop
     start
   ;;
   "status")
     status
   ;;
   "rename")
     rename
   ;;
   *)
     usage
   ;;
  esac

  

标签:grep,jar,pid,server,启停,linux,echo,JARNAME
From: https://www.cnblogs.com/zenghaoxhx/p/16623423.html

相关文章

  • Linux驱动开发1_虚拟字符设备
    字符设备开发步骤一、驱动模块的加载与卸载二、字符设备注册与注销三、实现设备的具体操作函数四、添加LICENSE和作者信息 Linux设备号一、设备号的组成二、设备......
  • linux开机启动执行脚本或命令
    添加到  /etc/rc.d/rc.local  给文件执行权限 /etc/rc.d/rc.local用于用户自定义开机启动程序,可以往里写开机要执行的命令或脚本,线上的配置如下:[root@loca......
  • Linux子系统之【进程调度】
    之前已知Linux对进程的管理现在看看Linux如何调度进程1.什么是进程调度?对于CPU来说,进程调度指的就是对CPU进行时间资源的分割管理。2.Linux中进程调度还是在调度进程吗......
  • Linux环境airflow安装部署
    pipinstallmysqlclient 环境:ubuntu,mysql5.7.37,python3.9.12,pip21.2.41.卸载原有airflow,并卸载;pipuninstallapache-airflow查找airflow相关文件......
  • Linux--常用命令
    基本命令ipaddr--------查看ip地址pwd----------显示当前全路径top----------查看进程的cpu、内存占用情况Ctrl+C-----------强制停止ps-ef|grep-i[进程名字]......
  • linux 解决vim无法正常打开文件
    如果在使用vim打开某个文件时出现以下内容,是因为上次打开文件后未正常关闭,留下了一个文件名加.swp后缀的文件(隐藏文件),删除这个.swp文件便能正常打开文件了E325:ATTENTION......
  • npm+react linux 开荒
    安装npmyuminstallnodejs.x86_64yuminstallnpm.x86_64 更新GCC版本(参考链接:https://blog.csdn.net/qq_39715000/article/details/120703444)升级到gcc7.3yum-y......
  • linux-文件权限
    一、/etc/passwd文件Linux系统使用一个专门的文件来将用户的登录名匹配到对应的UID值。这个文件就是/etc/passwd文件,它包含了一些与用户有关的信息。字段分别是:登录......
  • Linux 圈内导航
    欢迎来到Linux多彩世界 CentOS    |    Ubuntu    |    Kail   ......
  • Linux下的常用函数
    1.open函数open函数:用来打开或者创建一个文件或者设备。1.函数原型:intopen(constchar*pathname,intflags);intopen(constchar*pathname,intflags,mode_t......