对于java应用部署在linux环境分两种情况,一为war包,应用入口为编译后的classes文件下的本身应用入口,其启动脚本可参考不错的linux下通用的java程序启动脚本这篇博客。

    一为jar包形式

这种便于打包增量升级,关注于改动的jar即可。此应用的结构正是用微服务框架springboot快速搭建并配合以下shell脚本部署linux。

#!/bin/bash

#此脚本为Linux下启动java程序的通用脚本。(包含启动,停止,重启)
#cd 进入脚本执行的bin目录,sh server.sh start(启动) | stop(停止)| restart(重启)
#
#Author: ant.ding, Date: 2017/8/31
#

#bin目前路径以及相关目录路径
cd `dirname $0`
BIN_DIR=`pwd`
cd ..
DEPLOY_DIR=`pwd`
CONF_DIR=$DEPLOY_DIR/conf
LOGS_DIR=$DEPLOY_DIR/logs
LIB_DIR=$DEPLOY_DIR/lib
MAIN_CLASS=com.goldenway.small.Application

# config file dir
SERVER_NAME=DEPLOY_DIR

STDOUT_FILE=$LOGS_DIR/stdout.log

LOG_BACK="-Dlogback.configurationFile=conf/logback.xml"
cmd=$2
echo "$cmd"

start(){
    echo "CONF_DIR=$CONF_DIR"
	PIDS=`ps  --no-heading -C java -f --width 1000 | grep "$CONF_DIR" |awk '{print $2}'`
	if [ -n "$PIDS" ]; then
	   	 echo "ERROR: The $SERVER_NAME already started!"
	    echo "PID: $PIDS"
	    exit 1
	fi

	if [ ! -d $LOGS_DIR ]; then
		mkdir $LOGS_DIR
	fi


	LIB_JARS=`ls $LIB_DIR|grep .jar|awk '{print "'$LIB_DIR'/"$0}'|tr "\n" ":"`
	for xml in $DEPLOY_DIR/*.xml
		do	LIB_JARS=$LIB_JARS:$xml
	done
	for env in $DEPLOY_DIR/*.properties
		do	LIB_JARS=$LIB_JARS:$env
	done


	JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true "
	JAVA_DEBUG_OPTS=""
	if [ "$cmd" = "debug" ]; then
	    JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
	fi
	JAVA_JMX_OPTS=""
	if [ "$cmd" = "jmx" ]; then
		ip=$(ifconfig | grep "inet addr" | grep -v 127.0.0.1 | awk '{print $2}' | awk -F ':' '{print $2}')
	    JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false  -Djava.rmi.server.hostname=$ip "
	fi
	JAVA_MEM_OPTS=""
	BITS=`file $JAVA_HOME/bin/java | grep 64-bit`
	echo "BITS =$BITS"
	if [ -n "$BITS" ]; then
		  JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn256m -XX:PermSize=128m  -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 "
	else
		JAVA_MEM_OPTS=" -server -Xms1g -Xmx1g -XX:PermSize=128m -XX:SurvivorRatio=2 -XX:+UseParallelGC "
	fi

	echo "JAVA_MEM_OPTS=$JAVA_MEM_OPTS"
	echo "JAVA_JMX_OPTS=$JAVA_JMX_OPTS"

	echo -e "Starting the $SERVER_NAME ...\c"
	nohup java $JAVA_OPTS $JAVA_MEM_OPTS $LOG_BACK $JAVA_DEBUG_OPTS $JAVA_JMX_OPTS -classpath $DEPLOY_DIR:$LIB_DIR:$CONF_DIR:$LIB_JARS $MAIN_CLASS > /dev/null  2>>$STDOUT_FILE &

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

stop(){

		PIDS=`ps  --no-heading -C java -f --width 1000 | grep "$CONF_DIR" |awk '{print $2}'`
		if [ -z "$PIDS" ]; then
		    echo "ERROR: The $SERVER_NAME does not started!"
		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 --no-heading -p $PID`
				if [ -n "$PID_EXIST" ]; then
					COUNT=0
					break
				fi
			done
		done
		echo "stop OK!"
		echo "PID: $PIDS"
		  
}

case $1 in
  start)
		start;
    ;;
  stop)
		stop;
    ;;
  restart)
		echo "############ Application of '"$MAIN_CLASS"' restarting....############"
    stop;
    sleep 1
    start;
    ;;
  *)
    echo "Usage: startup.sh {start|stop|restart}" 
    ;;
esac
exit 0


Logo

更多推荐