很多时候我们部署jar包到服务器上的时候,都需要准备一个重启、查看jar包状态的sh脚本,本文分享一个。
拿去即可用,把它放到和jar包同一个目录里即可,可以给它取个名字叫app.sh。
一些说明:
1、第18行是启动jar包的命令,这个示例里指定了nacos地址和nacos环境,还指定了需要使用的内存大小,还指定了日志路径。你也可以根据你的需要增加启动参数。
2、变量`$appName`就是jar包名称。
3、restart也实现了备份jar包的功能。
4、使用的时候命令就输入app.sh restart或app.sh status等。
1 #!/bin/bash 2 ## Author 夏威夷8080 3 ## UPDATE 2019-04-15 4 version="1.0.0"; 5 6 appName=$2 7 if [ -z $appName ];then 8 appName=`ls -t |grep .jar$ |head -n1` 9 fi 10 11 function start() 12 { 13 count=`ps -ef |grep java|grep $appName|wc -l` 14 if [ $count != 0 ];then 15 echo "Maybe $appName is running, please check it..." 16 else 17 echo "The $appName is starting..." 18 nohup java -Dspring.profiles.active=dev -Dspring.profiles.address=111.22.33.4444:8848 -jar ./$appName -XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -Xms768m -Xmx1280m > ./logs/debug.log 2>&1 & 19 fi 20 } 21 22 function stop() 23 { 24 appId=`ps -ef |grep java|grep $appName|awk '{print $2}'` 25 if [ -z $appId ];then 26 echo "Maybe $appName not running, please check it..." 27 else 28 echo "The $appName is stopping..." 29 kill $appId 30 fi 31 } 32 33 function restart() 34 { 35 # get release version 36 releaseApp=`ls -t |grep .jar$ |head -n1` 37 38 # get last version 39 lastVersionApp=`ls -t |grep .jar$ |head -n2 |tail -n1` 40 41 appName=$lastVersionApp 42 stop 43 for i in {5..1} 44 do 45 echo -n "$i " 46 sleep 1 47 done 48 echo 0 49 50 backup 51 52 appName=$releaseApp 53 start 54 } 55 56 function backup() 57 { 58 # get backup version 59 backupApp=`ls |grep -wv $releaseApp$ |grep .jar$` 60 61 # create backup dir 62 if [ ! -d "backup" ];then 63 mkdir backup 64 fi 65 66 # backup 67 for i in ${backupApp[@]} 68 do 69 echo "backup" $i 70 mv $i backup 71 done 72 } 73 74 function status() 75 { 76 appId=`ps -ef |grep java|grep $appName|awk '{print $2}'` 77 if [ -z $appId ] 78 then 79 echo -e "\033[31m Not running \033[0m" 80 else 81 echo -e "\033[32m Running [$appId] \033[0m" 82 fi 83 } 84 85 86 function usage() 87 { 88 echo "Usage: $0 {start|stop|restart|status|stop -f}" 89 echo "Example: $0 start" 90 exit 1 91 } 92 93 case $1 in 94 start) 95 start;; 96 97 stop) 98 stop;; 99 100 restart) 101 restart;; 102 103 status) 104 status;; 105 106 *) 107 usage;; 108 esac
标签:grep,appName,stop,重启,jar,echo,sh,backup From: https://www.cnblogs.com/shamo89/p/16870543.html