java项目自动部署的启动和终止的脚本

前言

以下的启动脚本(start.sh)和终止脚本(stop.sh)参考自dubbo中提供的脚本。是为了方便自动部署,不需要手动的kill进程,防止出错。

启动脚本:start.sh


#!/bin/bash

# java home
export JAVA_HOME=/usr/java/jdk1.8.0_152

# get current dir
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf
LIB_DIR=$DEPLOY_DIR/lib
SERVER_NAME=`basename $DEPLOY_DIR`

PIDS=`ps -f | grep java | grep "$SERVER_NAME.jar" |awk '{print $2}'`
if [ -n "$PIDS" ]; then
    echo "ERROR: The $SERVER_NAME already started!"
    echo "PID: $PIDS"
    exit 1
fi

nohup $JAVA_HOME/bin/java -jar $SERVER_NAME.jar > /dev/null 2>&1 &

echo -e "Starting the $SERVER_NAME ...\c"
COUNT=0
while [ $COUNT -lt 1 ]; do    
    echo -e ".\c"
    sleep 1
    COUNT=`ps -f | grep java | grep "$SERVER_NAME.jar" | awk '{print $2}' | wc -l`
    if [ $COUNT -gt 0 ]; then
        break
    fi
done
echo "OK!"
PIDS=`ps -f | grep java | grep "$SERVER_NAME.jar" | awk '{print $2}'`
echo "PID: $PIDS"

终止脚本:stop.sh

#!/bin/bash

# java home
export JAVA_HOME=/usr/java/jdk1.8.0_152

# get current dir
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf
LIB_DIR=$DEPLOY_DIR/lib
SERVER_NAME=`basename $DEPLOY_DIR`

PIDS=`ps -f | grep java | grep "$SERVER_NAME.jar" |awk '{print $2}'`
if [ -z "$PIDS" ]; then
    echo "ERROR: The $SERVER_NAME does not started!"
    exit 1
fi

echo -e "Stopping the $SERVER_NAME ...\c"
for PID in $PIDS ; do
    kill $PID > /dev/null 2>&1
done

COUNT=0
while [ $COUNT -lt 1 ]; do    
    echo -e ".\c"
    sleep 1
    COUNT=1
    for PID in $PIDS ; do
        PID_EXIST=`ps -f -p $PID | grep java`
        if [ -n "$PID_EXIST" ]; then
            COUNT=0
            break
        fi
    done
done

echo "OK!"
echo "PID: $PIDS"

   转载规则


《java项目自动部署的启动和终止的脚本》 孤独如梦 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
spring boot配置druid数据库密码加密 spring boot配置druid数据库密码加密
前言本文主要介绍如何在spring boot中配置druid中数据库密码加密。 1. 引入druid的包 <dependency> <groupId>com.alibaba</groupId>
2019-06-06
下一篇 
Zookeeper不断提示连接被拒绝问题解决 Zookeeper不断提示连接被拒绝问题解决
Zookeeper不断提示连接被拒绝问题解决问题描述zookeeper启动后,查看zookeeper.out文件提示连接被拒,每次都是秒断,日志不断刷新。 2019-03-20 18:50:36,190 [myid:] - INFO [N
2019-06-06
  目录