The general java program under linux starts, closes and restarts the script

Pure handwriting, you only need to modify some configuration variables, you can use it as a startup script.

And in addition to being able to start, it also supports the functions of closing, restarting, and checking whether it is running.

Let me show you the effect:



 

 

 

#!/bin/bash
# MAINTAINER gaoyaohua "[email protected]" www.updn.cn
# tingyun service

APPNAME=dc-backend-server
CONF=spring-config-server.xml
APPDIR=$(dirname $(cd `dirname $0`; pwd))
TINGYUNDIR=$(dirname ${APPDIR})
JAVA_HOME=${TINGYUNDIR}/java
LIB=${APPDIR}/target/lib
CLASSPATH=${APPDIR}/target/conf
PROPS_FILE=${APPDIR}/conf/conf-deploy.properties
PROPS_ENCODING=UTF-8
LOGDIR=${APPDIR}/logs
APPPID=${APPDIR}/run/${APPNAME}.pid
LOCKFILE=${APPDIR}/lock/$APPNAME
SHUTDOWN_WAIT=1

# java options
JAVA_OPTS="-Xmx2048M -Xms512M -Xmn256M -XX:SurvivorRatio=1 -Xss256k -XX:PermSize=32M -XX:MaxPermSize=72M -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:CMSFullGCsBeforeCompaction=0 -XX:+CMSClassUnloadingEnabled -XX:LargePageSizeInBytes=128M -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=80 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+PrintClassHistogram  -verbose:gc -XX:+PrintGCDetails  -XX:+PrintGCTimeStamps  -XX:-HeapDumpOnOutOfMemoryError -Xloggc:"$LOGDIR"/$APPNAME-gc.log -XX:HeapDumpPath="$LOGDIR"/$APPNAME-dump"

# jmxremote options default comment is off
#JMX_OPTS=-Dcom.sun.management.jmxremote.port=10004 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

# enter main class start
ENTER="com.networkbench.newlens.datacollector.Bootstrap"


if [ -d "$LIB" ]; then
    for i in "$LIB"/*.jar; do
      CLASSPATH="$CLASSPATH":"$i"
    done
be


start() {
	kpid="`ps x | grep "DappName=$APPNAME" | grep -v grep | awk '{print $1}'`"
	if [ -n "$kpid" ] && [ -f $LOCKFILE ] && [ -f $APPPID ];then
		read kpid < $APPPID
		echo -e "$APPNAME is \033[32m running \033[0m"
		return 0
	be

	echo -n "$APPNAME start  " && echo -ne "\033[s ."
	nohup $JAVA_HOME/bin/java -DappName="$APPNAME" $JAVA_OPTS -Dtingyunpath="$APPDIR" -Dsharedpath="$TINGYUNDIR/shared" -Dwork.dir="$APPDIR" -Dconf=$CONF -Dprops=$PROPS_FILE -DpropsEncoding=$PROPS_ENCODING -Dnohup=true -Dpid=$$ -Dpid.file=$APPPID -DdiagnoseOnSignal=ALRM  $JMX_OPTS -cp $CLASSPATH  $ENTER >> "$LOGDIR"/nohup_"$APPNAME" 2>&1 &
	RETVAL=$? && [ $RETVAL = 0 ] && touch $LOCKFILE && echo -e "\033[u\033[K [  \033[32m ok \033[0m  ]"
    	echo `date "+%F %T"` $APPNAME started. >> "$LOGDIR"/$APPNAME-restart.log
    	return $RETVAL
}


stop() {
	kpid="`ps x | grep "DappName=$APPNAME" | grep -v grep | awk '{print $1}'`"
	#if [ ! -n "$kpid" ] && [ ! -f $LOCKFILE ] && [ ! -f $APPID ];then
	if [ ! -n "$kpid" ];then
		echo -e "$APPNAME is \033[31m not running \033[0m"
		return 1
	be
	count=-1
	if [ -f $LOCKFILE ] && [ -f $APPID ]; then
		read kpid < $APPPID
		echo -n -e "$APPNAME stop   " && echo -n -e "\033[s";
		kill $kpid
            	read kpid < $APPPID
		count=0;
            	let kwait=$SHUTDOWN_WAIT
            	until [ `ps --pid $kpid | grep -c $kpid` = '0' ] || [ $count -gt $kwait ]
            	do
                	echo -n -e ".";
                	sleep 1
                	let count=$count+1;
            	done
	be
        if [ $count -gt $kwait ] && [ "$count" -eq "-1" ];then
        	echo -e "\033[u\033[K [\033[31m failed \033[0m]"
		return -1
		#kill -9 $kpid
        be
        if [ $count -le $kwait ]; then
        	echo -e "\033[u\033[K [  \033[32m ok \033[0m  ]"
        be
    	rm -f $LOCKFILE $APPPID
}

status() {
	kpid="`ps x | grep "DappName=$APPNAME" | grep -v grep | awk '{print $1}'`"
	if [ -n "$kpid" ];then
		if [ -f $LOCKFILE ] && [ -f $APPID ];then
			echo -e "$APPNAME is \033[32m running \033[0m"
		else
			echo -e "$APPNAME is \033[33m running \033[0m but lockfile or pidfile not ready"
		be
	else
		echo -e "$APPNAME is \033[31m not running \033[0m"
	be
}

info() {
	echo $APPNAME
}



case "$1" in
	'start')
		start
		;;
	'stop')
		stop
		;;
	'restart')
		stop
		start
		;;
	'status')
		status
		;;
	'info')
		info
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status|info}"
		exit 1
		;;
esac
exit 0

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326171218&siteId=291194637