java应用启动与停止的shell脚本分享
更新时间:2023年10月23日 08:08:03 作者:技术驿站
这篇文章主要和大家分享三个shell脚本,分别是jvm启动参数相关的setjvment.sh,利用java命令启动进程的start.sh和停止java进程的stop.sh,有需要的可以参考下
整体思路
基于shell脚本开发3个脚本,分别是
- jvm启动参数相关的setjvment.sh,主要是jvm启动相关参数配置
- start.sh,主要是利用java命令启动进程,包含classpath和执行结果日志保存相关设置
- stop.sh,主要是停止java进程,包括优雅停止和强制停止
如果linux没有安装jdk,可使用如下命令安装jdk:
sudo apt update sudo apt install openjdk-8-jdk
然后在启动的文件.bash_profile中设置环境变量JAVA_HOME,
#!/bin/sh export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
相关脚本
setjvm
#!/bin/bash # 设置Java堆栈大小 JAVA_OPTS="$JAVA_OPTS -Xmn512m -Xms512m -Xmx1024m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m" JAVA_OPTS="$JAVA_OPTS -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=8 -XX:-UseAdaptiveSizePolicy" # 垃圾收集器设置 JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=60" JAVA_OPTS="$JAVA_OPTS -XX:+CMSClassUnloadingEnabled" # 打印GC日志 JAVA_OPTS="$JAVA_OPTS -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCApplicationStoppedTime -XX:+UseGCLogFileRotation" JAVA_OPTS="$JAVA_OPTS -XX:+PrintHeapAtGC -Xloggc:$work_home/logs/gc.log -XX:GCLogFileSize=1m -XX:NumberOfGCLogFiles=10" # 打印DUMP日志 JAVA_OPTS="$JAVA_OPTS -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$work_home/logs" # 其他设置 JAVA_OPTS="$JAVA_OPTS -XX:+DisableExplicitGC -XX:+UseFastAccessorMethods -XX:-UseBiasedLocking" # 指定随机数生成器的种子源 JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/urandom" # 设置java.ext.dirs JAVA_OPTS="$JAVA_OPTS -Djava.ext.dirs=$JAVA_HOME/jre/lib/ext:$work_home/lib" # 其他环境变量或配置设置 # export OTHER_ENV_VARIABLE=example_value
start
#!/bin/bash CUR="$0" while [ -h "$CUR" ]; do ls=$(ls -ld "$CUR") link=$(expr "$ls" : '.-> (.)$') if expr "$link" : '/.*' >/dev/null; then CUR="$link" else CUR=$(dirname "$CUR")/"$link" fi done CUR_DIR=$(dirname "$CUR") work_home=$( cd "$CUR_DIR/.." >/dev/null pwd ) if [ -z $JAVA_HOME ]; then echo "plz set JAVA_HOME env." exit 1 fi if [ -r "$work_home/bin/setenv.sh" ]; then . "$work_home/bin/setjvmenv.sh" fi CLASSPATH=$CLASSPATH:$work_home/lib/conf LOG_DIR="$work_home/logs" if [ ! -d $LOG_DIR ]; then mkdir "$LOG_DIR" fi main_class="com.toby.dynamic.data.source.start.PointApplication" count=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|wc -l` if [ $count != 0 ]; then app_id=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|awk '{print $2}'` echo "already running with pid $app_id, start aborted." else echo "use java_home $JAVA_HOME" echo "use work_home $work_home" exec $JAVA_HOME/bin/java $JAVA_OPTS -cp $CLASSPATH \ -Dwork.home=$work_home -Djava.io.tmpdir=$work_home \ $main_class >$LOG_DIR/server.log 2>&1 & echo "server started." fi
stop
#!/bin/bash PRG="$0" while [ -h "$PRG" ]; do ls=$(ls -ld "$PRG") link=$(expr "$ls" : '.-> (.)$') if expr "$link" : '/.*' >/dev/null; then PRG="$link" else PRG=$(dirname "$PRG")/"$link" fi done PRGDIR=$(dirname "$PRG") work_home=$( cd "$PRGDIR/.." >/dev/null pwd ) main_class="com.toby.dynamic.data.source.start.PointApplication" count=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|wc -l` echo " stop server start." if [ $count == 0 ]; then echo "already stop,return." exit 0 fi app_id=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|awk '{print $2}'` # 请求优雅退出 kill -15 $app_id SLEEP=5 STOPPED=0 while [ $SLEEP -ge 0 ]; do count=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|wc -l` if [ $count == 0 ]; then STOPPED=1 echo " server stopped." break; fi if [ $SLEEP -gt 0 ]; then sleep 1 fi if [ $SLEEP -eq 0 ]; then echo "server did not stopped in time." fi SLEEP=`expr $SLEEP - 1 ` done if [ $STOPPED -eq 0 ]; then echo "server killed fore" SLEEP=5 kill -15 $app_id while [ $SLEEP -ge 0 ]; do count=`ps -ef|grep java|grep $work_home|grep $main_class|grep -v grep|wc -l` if [ $count == 0 ]; then STOPPED=1 echo " server has been killed." break; fi if [ $SLEEP -gt 0 ]; then sleep 1 fi if [ $SLEEP -eq 0 ]; then echo "server did not stopped in time." fi SLEEP=`expr $SLEEP - 1 ` done if [ $STOPPED -eq 0 ]; then echo "server has not been killed completely yet." fi fi
执行结果
以上就是java应用启动与停止的shell脚本分享的详细内容,更多关于java应用启动与停止的资料请关注脚本之家其它相关文章!
相关文章
在linux shell脚本中root切换到普通用户执行脚本或命令的方法
今天小编就为大家分享一篇在linux shell脚本中root切换到普通用户执行脚本或命令的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-06-06
最新评论