首页 > 系统相关 >Linux部署系统

Linux部署系统

时间:2022-11-02 12:00:57浏览次数:38  
标签:grep start 部署 系统 jar echo Application result Linux

部署系统

1、在/home/ 目录下创建要部署的服务的名称;

2、在idea的maven中先clean,禁用test选项,后package打包,则可以在target目录中得到jar包文件;

image-20221024142331455

3、将生成的jar包文件拷入/home/filename/中,将application.yml拷入config文件中;

image-20221024142652049

4、sh app.sh start 运行app.sh文件则可以启动系统,watch.out为启动日志。

app.sh文件内容:

#!/bin/sh

echo "======================================="
echo "Shell: $0"

# 获得本地路径
CURR_FOLDER=`pwd`
echo "Application folder: ${CURR_FOLDER}"
cd ${CURR_FOLDER}

# 获得jar文件的名字
JAR_NAME=`ls -l ${CURR_FOLDER} | grep .jar | grep -v grep | awk '{print $9}'`
echo "Application jar file: ${JAR_NAME}"

echo "======================================="

OUT_FILE="watch.out"

# 函数部分
hasrun(){
  pid=`ps --no-heading -ef | grep ${JAR_NAME} | grep -v grep | awk '{print $2}'`
  # -n 判断字符串长度不为0 -z 判断字符串长度为0
  if [ -z ${pid} ]; then    
    echo 0
  else    
    echo ${pid}
  fi
}

start(){
    # 是否已运行    
    result=$(hasrun)
    
    if [ $result -ne "0" ]; then
      echo "Application has already started, the pid is $result"
      exit 1   
    else
      # 启动程序
      echo "Starting application ... " 
      nohup java -jar ${JAR_NAME} > ${OUT_FILE} 2>&1 &      
      echo "Application has started, the pid is $! you can check file in ${OUT_FILE} for deatils."     
    fi
}

stop(){
    # 是否已运行
    result=$(hasrun)
    
    if [ $result -eq "0" ]; then
      # 程序没有运行
      echo "Application is not running ..."
      exit 1      
    else    
      # 程序在运行中
      echo "Stopping application ..."    
      kill ${result}
      
      # 用循环的方式检测程序是否关闭
      count=0
      while [ $count -lt 1 ]; do
        sleep 1s
        count=0
        exist=`ps --no-heading -p ${result}`
        if [ -z ${exist} ] ; then          
          count=1
          echo "Applicatoin has stoped." 
        fi
      done
      
      
    fi    
}

restart(){
  stop
  start  
}


case $1 in
"start")
  start
  ;;
"stop")
  stop
  ;;
"restart")
  restart
  ;;
*)
  echo "there is no command"
  exit 1;
  ;;
esac

标签:grep,start,部署,系统,jar,echo,Application,result,Linux
From: https://www.cnblogs.com/coolsheep/p/16850598.html

相关文章