随笔- 21  文章- 0  评论- 0  阅读- 12657 

 

前段时间用springboot做项目后,每次重新发布都好麻烦, 所以写了个脚本来配合jenkins 发布;

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
 
 
APP_NAME=application.jar
 
function check {
    local PID=0
    if [ -e tpid ] ;then
        PID=$(cat tpid)
        # while read TMPID; do PID=$TMPID ;done < tpid
    fi
    # echo "PID:$PID"
    if ps -p $PID > /dev/null ;then
        echo $PID
    else
        echo 0
    fi
}
function isRun {
    local PID=$(check)
    if [ $PID -ne 0 ]; then
        echo "application is running..."
    else
        echo "application is not running..."
    fi
}
  
function start {
    if [ ! -e $APP_NAME ]; then
        echo "$APP_NAME is not found!"
        return 0
    fi
 
    local PID=$(check)
    if [ $PID -ne 0 ]; then
        echo "$PID  $APP_NAME was running..."
        return 0
    fi
 
    local argumet=""
    if [ -e application.properties ] ;then
        argumet="$argumet --spring.config.location=application.properties"
    fi
    nohup java -jar $APP_NAME $argumet > log.out 2>&1 &
    echo $! > tpid
    PID=$(cat tpid)
    echo $PID Start Success!
}
 
function stop {
    local PID=$(check)
    if [ $PID -ne 0 ]; then
        echo -n "Stop Process..."
        kill -15 $PID
        num=0
        while [[ num -le 5 ]]; do
            # echo $num
            echo -n "."
            sleep 1
            PID=$(check)
            if [ $PID -eq 0 ]; then
                echo "Success!"
                return 0
            fi
            num=$[ $num + 1 ]
        done
        PID=$(check)
        if [ $PID -ne 0 ]; then
            kill -9 $PID
            echo ""
            echo "Kill Process!"
        else
            echo "Success!"
        fi
        return 0
    fi
    return 1
}
 
function restart {
    stop
    start
}
 
function deploy {
    fd=$(date +"%Y%m%d%H%M%S")
    fileCount=`ls ./target/ | grep .jar$ | wc -l`
    if [ $fileCount -eq 1 ]; then 
        fileName=`ls ./target/ | grep .jar$`
        newName="$fileName.$fd"
        mv ./target/$fileName ./target/$newName
        if [ -e ./$APP_NAME ]; then
            rm -rf ./$APP_NAME
        fi
        ln -s ./target/$newName ./$APP_NAME
        echo "deploy success..."
        restart
    else
        echo "Can not be deploy, jar file $fileCount"
        return 1
    fi
}
 
 
case $1 in
    "start" ) start;;
    "stop" ) stop;;
    "check" ) isRun;;
    "restart" ) restart;;
    "deploy" ) deploy;;
    * ) echo "$0 start"
        echo "$0 stop"
        echo "$0 check"
        echo "$0 restart"
        echo "$0 deploy"
esac
 
exit 0

  

 

 posted on   xdsfoo  阅读(774)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示