美文网首页Linux专属
shell 脚本快捷运行 jar 文件

shell 脚本快捷运行 jar 文件

作者: 风中吃西瓜 | 来源:发表于2019-03-08 22:01 被阅读0次
    • 创建run.sh
    touch run.sh
    
    • 编辑脚本内容
    vim run.sh
    
    #!/bin/bash
    #description: 启动重启server服务 
    #端口号,根据此端口号确定PID  
    PORT=8080
    #启动命令所在目录  
    HOME='/opt/jars/test'
    
    #查询出监听了test.jar端口TCP协议的程序  
    pid=$(ps -ef | grep test.jar | grep -v grep | awk '{print $2}')
    
    start(){
       echo "start running cloud-core ............... "  
       if [ -n "$pid" ]; then
          echo "server already start,pid:$pid"  
          echo "pid:$pid test port:$PORT 服务已经在运行了,请停止后再 执行 sh run.sh start "  
          return 0
       fi
       #进入命令所在目录  
       cd $HOME
       # 启动服务控制台日志输出到nohup.out文件中
       nohup java -jar test.jar >nohup.out 2>&1 &
       echo "running success test port:$PORT"
       echo "test port:$PORT 服务启动成功 ..... "  
    }
    
    stop(){
       echo "stopping running cloud-core ............... "  
       if [ -z "$pid" ]; then
          echo "not find program on port:$PORT"  
          echo "test port:$PORT 服务已经被关闭了请执行 sh run.sh start "   
          return 0
       fi
       #结束程序,使用讯号2,如果不行可以尝试讯号9强制结束  
       kill -9 $pid
       rm -rf $pid
       echo "kill program use signal 2,pid:$pid"  
    }
    
    status(){
       if [ -z "$pid" ]; then
          echo "not find program on port:$PORT"  
       else
          echo "program is running,pid:$pid"  
       fi
    }
    
    case $1 in
       start)
          start
       ;;
       stop)
          stop
       ;;
       restart)
          $0 stop
          sleep 2
          $0 start
        ;;
       status)
          status
       ;;
       *)
          echo $"Usage: $0 {start|stop|status}"  
          exit 0
    esac
    
    • 检测脚本格式
      直接输入":",然后在":"之后输入"set ff"如下图所示


      image.png

      如果显示fileformat=doc我们需要把格式改为unix,方法是输入":set ff=unix",也可以输入":set fileformat=unix"

    • 启动程序

    # 切换到脚本所在目录
    cd /opt/jars/test
    sh run.sh start
    
    • 停止程序
    # 切换到脚本所在目录
    cd /opt/jars/test
    sh run.sh stop
    
    • 重启程序
    # 切换到脚本所在目录
    cd /opt/jars/test
    sh run.sh restart
    

    *查看控制台日志

    # 切换到脚本所在目录
    cd /opt/jars/test
    tail -f nohup.out
    

    相关文章

      网友评论

        本文标题:shell 脚本快捷运行 jar 文件

        本文链接:https://www.haomeiwen.com/subject/ryodpqtx.html