java启动和停止sh

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://ligang.blog.csdn.net/article/details/43453083

一、说明

window下:echo "%JAVA_HOME%" Liunx Crontab 定时的逻辑:/etc/crontab JAVA_HOME/bin/:java -help java:Java解释器,直接从类文件执行Java应用程序代码 jps:仅查找当前用户的Java进程,而不是当前系统中的所有进程。 native2ascii:用于转换字符或者文件的编码格式

二、启动脚本

#!/bin/sh

#set JAVA_HOME
#JAVA_HOME=/usr/local/java

#check JAVA_HOME & java
noJavaHome=false
# -z 字符串长度伪则为真
if [ -z "$JAVA_HOME" ] ; then
    noJavaHome=true
fi
# -e 如果文件存在则为真
if [ ! -e "$JAVA_HOME/bin/java" ] ; then
    noJavaHome=true
fi
if $noJavaHome ; then
    echo
    echo "Error: JAVA_HOME environment variable is not set."
    echo
    exit 1
fi
#==============================================================================

#set JAVA_OPTS
JAVA_OPTS="-server -Xms2048m -Xmx2048m -Xmn512m -Xss512k"
#performance Options
JAVA_OPTS="$JAVA_OPTS -XX:+AggressiveOpts"
JAVA_OPTS="$JAVA_OPTS -XX:+UseBiasedLocking"
JAVA_OPTS="$JAVA_OPTS -XX:+UseFastAccessorMethods"
JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC"
JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC"
JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC"
JAVA_OPTS="$JAVA_OPTS -XX:+CMSParallelRemarkEnabled"
JAVA_OPTS="$JAVA_OPTS -XX:+UseCMSCompactAtFullCollection"
JAVA_OPTS="$JAVA_OPTS -XX:+UseCMSInitiatingOccupancyOnly"
JAVA_OPTS="$JAVA_OPTS -XX:CMSInitiatingOccupancyFraction=75"
#GC Log Options
#JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCApplicationStoppedTime"
#JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCTimeStamps"
#JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails"
#debug Options
#JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=8065,server=y,suspend=n"
#==============================================================================

#set HOME
PTFENCE_DIR=`pwd`
#这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这个目录来定位所要运行程序的相对位置(绝对位置除外)。
cd `dirname "$0"`/..
PTFENCE_HOME=`pwd`
cd $PTFENCE_DIR
if [ -z "$PTFENCE_HOME" ] ; then
    echo
    echo "Error: COBAR_HOME environment variable is not defined correctly."
    echo
    exit 1
fi
#==============================================================================

#set CLASSPATH
PTFENCE_CLASSPATH="$PTFENCE_HOME/conf:$PTFENCE_HOME/lib/classes"
for i in "$PTFENCE_HOME"/lib/*.jar
do
    PTFENCE_CLASSPATH="$PTFENCE_CLASSPATH:$i"
done
#==============================================================================

if [ -d "$PTFENCE_HOME/logs" ] ; then
	echo "logs dir exits"
else 
	mkdir  $PTFENCE_HOME/logs
fi

#startup Server
#get java
RUN_CMD="\"$JAVA_HOME/bin/java\""
#-D<name>=<value> set a system property
RUN_CMD="$RUN_CMD -Dptfence.home=\"$PTFENCE_HOME\""
#-classpath:class search path of directories and zip/jar files
RUN_CMD="$RUN_CMD -classpath \"$PTFENCE_CLASSPATH\""
#添加其他可扩展参数
RUN_CMD="$RUN_CMD $JAVA_OPTS"
#$@传递给函数或脚本的所有参数
RUN_CMD="$RUN_CMD com.test.inf.ptfence.FenceCore \"$PTFENCE_HOME/conf/config.properties\" $@"
#>>追加到文件末尾
RUN_CMD="$RUN_CMD >> \"$PTFENCE_HOME/logs/console.log\" 2>&1 "
echo $RUN_CMD
#eval的作用是再次执行命令行处理,也就是说,对一个命令行,执行两次命令行处理
eval $RUN_CMD
echo "====exec ptfence complete===="
#上个命令的退出状态,或函数的返回值
echo $?
#==============================================================================

三、停止脚本

#!/bin/sh

#set JAVA_HOME
#JAVA_HOME=/usr/local/java

#check JAVA_HOME & java
echo "start shutdown ptfence....."
noJavaHome=false
if [ -z "$JAVA_HOME" ] ; then
    noJavaHome=true
fi
if [ ! -e "$JAVA_HOME/bin/java" ] ; then
    noJavaHome=true
fi
if $noJavaHome ; then
    echo
    echo "Error: JAVA_HOME environment variable is not set."
    echo
    exit 1
fi
#==============================================================================

#set JAVA_OPTS
JAVA_OPTS="-Xss256k"
#==============================================================================

#stop Server
#jps仅查找当前用户的Java进程,而不是当前系统中的所有进程
$JAVA_HOME/bin/jps |grep FenceCore|awk -F ' ' '{print $1}'|while read line
do
  eval "kill -9 $line"
done
rm -rf /tmp/ptfence-*
#==============================================================================
echo "shutdown ptfence success."


jps

17930 Bootstrap
32502 SocketServer
23932 Jps
13193 Bootstrap
23219 Bootstrap
10282 Bootstrap
23907 FenceCore
26118 QuorumPeerMain
#awk -F ' ' '{print $1}' 第一段,即12345
#while read line命令从grep的输出中读取数据。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券