Shell脚本_启动停止重启sh脚本

时间:2021-10-11 22:59:39
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
#!/bin/sh
 
## java env
export JAVA_HOME=/usr/java/jdk1.7.0_79
export JRE_HOME=$JAVA_HOME/jre
 
## service name
## 服务所在目录
SERVICE_DIR=/usr/local/services/dubbodemo
## 服务名称
SERVICE_NAME=dub-service-demo
JAR_NAME=$SERVICE_NAME\.jar
PID=$SERVICE_NAME\.pid
 
cd $SERVICE_DIR
 
case "$1" in
 
    start)
        ##nohup &  以守护进程启动
        nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar $JAR_NAME >/dev/null 2>&1 &
        echo $! > $SERVICE_DIR/$PID
        echo "=== start $SERVICE_NAME"
        ;;
 
    stop)
        kill `cat $SERVICE_DIR/$PID`
        rm -rf $SERVICE_DIR/$PID
        echo "=== stop $SERVICE_NAME"
 
        ## 停止5秒
        sleep 5
        ##
        P_ID=`ps -ef | grep -w "$SERVICE_NAME" | grep -v "grep" | awk '{print $2}'`
        if [ "$P_ID" == "" ]; then
            echo "=== $SERVICE_NAME process not exists or stop success"
        else
            echo "=== $SERVICE_NAME process pid is:$P_ID"
            echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID"
            kill -9 $P_ID
        fi
        ;;
 
    restart)
        $0 stop
        sleep 2
        $0 start
        echo "=== restart $SERVICE_NAME"
        ;;
 
    *)
        ## restart
        $0 stop
        sleep 2
        $0 start
        ;;
 
esac
exit 0