Linux编辑启动中止重启springboot jar包脚本

 springboot的配置文件中,配置文件的名字都有各自的意义跟用途html

  1. dev 开发环境
  2. prod 生产环境(默认)
  3. test 测试环境

加载指定配置文件 --spring.profiles.active=prodjava

springboot加载jar包的方式有web

// 直接在控制台进行启动,缺点就是控制台关闭项目也就关闭了。 java -jar bootdo.jar // 这种方式能够运行在后台,可是若是推出了shell的话,那也会挂 java -jar /bootdo-2.0.0.jar > bootdolog.file 2>&1 & // 加上nohup的话,即便推出shell,也不影响。 nohup java -jar /bootdo-2.0.0.jar > bootdolog.file 2>&1 &

解释spring

nohup表示永久运行。&表示后台运行shell

> 表明重定向到哪里springboot

1 表示stdout标准输出,系统默认值是1,因此">/dev/null"等同于"1>/dev/null"
2 表示stderr标准错误bash

nohup ./mqnamesrv >/home/cxb/mqnamesrv.out 2>&1 & 即标准输出到mqnamesrv.out中,接着,标准错误输出重定向等同于标准输出,输出到同一文件中。服务器

 

在服务器上经过以下方式启动成功以后,若是涉及到从新启动,那么你须要经过ps -ef | grep bootdo 查询到进程号,再经过kill -s 9 ${pid} 进行杀死再从新启动,非常麻烦。websocket

nohup java -jar /bootdo-2.0.0.jar > bootdolog.file 2>&1 &

一两次还好说,若是涉及到屡次,那就有些崩溃了。socket

这样,能够经过编写一个shell脚原本进行启动(start)中止(stop)重启(restart)操做,一步到位,方便高效

 

在自定义目录建立 wss.sh 脚本,编辑内容以下。

 1 #!/bin/bash  2 #这里可替换为你本身的执行程序,其余代码无需更改  3 APP_NAME=websocketserver-0.0.1-SNAPSHOT.jar  4  5 #使用说明,用来提示输入参数  6 usage() {  7 echo "Usage: sh 脚本名.sh [start|stop|restart|status]"  8 exit 1  9 } 10 11 #检查程序是否在运行 12 is_exist(){ 13 pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` 14 #若是不存在返回1,存在返回0  15 if [ -z "${pid}" ]; then 16 return 1 17 else 18 return 0 19  fi 20 } 21 22 #启动方法 23 start(){ 24  is_exist 25 if [ $? -eq "0" ]; then 26 echo "${APP_NAME} is already running. pid=${pid} ." 27 else 28 nohup java -jar /mnt/ssd1/project/websocket/$APP_NAME > /mnt/ssd1/project/websocket/websocketserverlog.file 2>&1 & 29 echo "${APP_NAME} start success" 30  fi 31 } 32 33 #中止方法 34 stop(){ 35  is_exist 36 if [ $? -eq "0" ]; then 37 kill -9 $pid 38 else 39 echo "${APP_NAME} is not running" 40  fi 41 } 42 43 #输出运行状态 44 status(){ 45  is_exist 46 if [ $? -eq "0" ]; then 47 echo "${APP_NAME} is running. Pid is ${pid}" 48 else 49 echo "${APP_NAME} is NOT running." 50  fi 51 } 52 53 #重启 54 restart(){ 55  stop 56  start 57 } 58 59 #根据输入参数,选择执行对应方法,不输入则执行使用说明 60 case "$1" in 61 "start") 62  start 63  ;; 64 "stop") 65  stop 66  ;; 67 "status") 68  status 69  ;; 70 "restart") 71  restart 72  ;; 73 *) 74  usage 75  ;; 76 esac

 

在标红接头行配置启动命令。

以后就能够经过 wss.sh start | stop | restart 实现启动,中止,重启操做了。

 

补充下

sh xxx.sh与./xxx.sh区别

sh xxx.sh 是不须要有执行权限

./xxx.sh 是须要有执行权限的,能够经过 chmod +x xxx.sh 赋予权限

 

 

 

原文出处:https://www.cnblogs.com/chywx/p/10460061.html

相关文章
相关标签/搜索