Linux 简单的Java sh脚本启动jar包

 1  2  3 projectName="你的项目名"
 4 
 5 #提醒功能
 6 help() {
 7     echo "help: sh ${projectName}.sh [start|stop|restart]"
 8     exit 1
 9 }
10 
11 #判断项目是否运行,并且返回对应数字(0 为未启动 1为启动)
12 is_exist(){
13     #获取pid的方式,个人喜欢咋写就咋写
14     pid=`ps -ef|grep ${projectName}.jar|grep -v grep|awk '{print $2}'`
15     if [ -z "${pid}" ]
16     then
17         return 0
18     else
19         return 1
20     fi
21 }
22 
23 #开始运行项目
24 start(){
25     is_exist
26     #如果正在运行直接提示
27     if [ $? -eq 1 ]
28     then
29         echo "${projectName} is running"
30         exit 0;
31     else
32         #没有运行则运行项目
33         echo "start running ${projectName} ..."
34         currentPath=`pwd`
35         startPath=$(cd `dirname $0`;pwd)
36         #这里写的比较简单,实际生产环境,需要配置参数
37         cd ${startPath}
38         nohup java -jar -server ${projectName}.jar --spring.profiles.active=test > ${currentPath}/run.log 2>&1 &
39         cd ${currentPath}
40     fi
41 }
42 
43 #停止项目
44 stop(){
45     echo "stop $projectName ..."
46     is_exist
47     if [ $? -eq 1 ]
48     then
49         #通过pid关闭
50         kill -9 $pid
51         echo  "[ok]"
52     else
53         echo  "[ok]"
54     fi
55 }
56 
57 # 选择运行方式
58 case "$1" in
59 start)
60     start
61     ;;
62 stop)
63     stop
64     ;;
65 restart)
66     stop
67     start
68     ;;
69 *)
70     help
71     ;;
72 esac

 

posted @ 2022-10-08 16:36  深水是沉默  阅读(646)  评论(0编辑  收藏  举报