Ops:jar包启动关闭脚本
简介
公司开发架构为java语言的rpc dubbo架构,将功能分解为各个模块,模块较多,发布到环境上的应用为编译后的jar包和配置文件,以及启动关闭jar包的shell脚本。之前经常会出现进程启动不了,或程序启动了多个的情况,此次,做了优化,结构比较严谨,代码量少,就不放到github上了,在这里做个分享,希望对你有用。
环境介绍
此脚步使用场景需要标准化环境的要求,所有jar皆可使用该脚本使用,通用性良好。
- jdk环境安装路径为/usr/local/jdk
- 发布到服务器上的目录为/data/server/appname/下
- 目录名即为jar包名,jar包名称为appname.jar
start.sh启动脚本
#!/bin/sh
# William Guozi
# https://www.cnblogs.com/William-Guozi, williamguozi.github.io
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
SHELL=/bin/sh
JAVA_HOME=/usr/local/jdk
export PATH="$PATH:$JAVA_HOME/bin"
#获取当前目录名,即为appname
current_dir=`echo $(dirname $(readlink -f "$0"))`
jar_name=`echo $current_dir | awk -F/ '{print $NF}'`
config_file_name=application.properties
#筛选是否含有含有相关进程,如果有,执行结果为0,如果没有,则为其他值
jps | grep $jar_name.jar &> /dev/null
if [ $? -eq 0 ];then
sh $current_dir/stop.sh
sh $current_dir/start.sh
else
/usr/local/jdk/bin/java -jar \
-Xms1024m \
-Xmx1024m \
-XX:MetaspaceSize=128m \
-XX:MaxMetaspaceSize=128m \
-Xmn256m \
-XX:SurvivorRatio=8 \
-XX:+UseConcMarkSweepGC \
-XX:+UseCMSCompactAtFullCollection \
-XX:CMSMaxAbortablePrecleanTime=5000 \
-XX:+CMSClassUnloadingEnabled \
-XX:CMSInitiatingOccupancyFraction=80 \
-XX:CMSFullGCsBeforeCompaction=5 \
-XX:+DisableExplicitGC \
-XX:+DisableExplicitGC \
-verbose:gc \
-Xloggc:/data/oom/gc.log \
-XX:+PrintGCDetails \
-XX:+PrintGCDateStamps \
-XX:+UseCompressedOops \
-Djava.awt.headless=true \
-Dsun.net.client.defaultConnectTimeout=10000 \
-Dsun.net.client.defaultReadTimeout=30000 \
-Dfile.encoding=UTF-8 \
-XX:+TieredCompilation \
-XX:CICompilerCount=4 \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/data/oom/ \
/data/server/$jar_name/$jar_name.jar \
--spring.config.location=file:$current_dir/$config_file_name >/dev/null 2>&1 &
jps | grep $jar_name.jar &> /dev/null
[ $? -eq 0 ] && echo "$jar_name 已经启动" || echo "$jar_name 启动失败"
fi
stop.sh关闭脚本
#!/bin/sh
# William Guozi
# https://www.cnblogs.com/William-Guozi, williamguozi.github.io
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
SHELL=/bin/sh
JAVA_HOME=/usr/local/jdk
export PATH="$PATH:$JAVA_HOME/bin"
#获取当前目录名,即为appname
current_dir=`echo $(dirname $(readlink -f "$0"))`
jar_name=`echo $current_dir | awk -F/ '{print $NF}'`
pidarray=`jps | grep $jar_name.jar | awk -F' ' '{print $1}'`
for pid in $pidarray; do
kill -9 $pid
echo "$jar_name 已关闭"
done
if [ -z $pidarray ];then
echo "$jar_name pid 未找到"
fi
参考文档
Linux命令大全:http://www.runoob.com/linux/linux-command-manual.html