linux下通用的java程序启动关闭重启脚本
程序员文章站
2022-05-21 12:58:48
...
纯手写,只需要修改一些配置变量,就可以用来做起动脚本了。
并且除了能起动、还支持关闭、重启、查看是否正在运行的功能。
给大家演示一下效果:
#!/bin/bash # MAINTAINER gaoyaohua "gaoyh@tingyun.com" 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 默认注释为关掉 #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 fi 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 fi 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 fi 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 fi if [ $count -gt $kwait ] && [ "$count" -eq "-1" ];then echo -e "\033[u\033[K [\033[31m failed \033[0m]" return -1 #kill -9 $kpid fi if [ $count -le $kwait ]; then echo -e "\033[u\033[K [ \033[32m ok \033[0m ]" fi 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" fi else echo -e "$APPNAME is \033[31m not running \033[0m" fi } 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