侧边栏壁纸
博主头像
Ric

Talk less,think more,do more

  • 累计撰写 22 篇文章
  • 累计创建 9 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

【工具-Shell脚本】java程序产品包模板-linux和windows通用shell启动停止脚本(无需系统安装Java运行环境)

Ric
Ric
2021-10-18 / 0 评论 / 3 点赞 / 1,352 阅读 / 4,296 字 / 正在检测是否收录...
温馨提示:
若内容或图片失效,请留言反馈。

此模板已打包上传,可直接点击下载

产品包目录结构

目录结构
source
config

Linux脚本

  1. 替换source/application.jar为你的jar;
  2. 修改start.sh中第3行的APP_NAME为你的jar名称;
  3. 修改stop.sh中第2行的APP_NAME为你的jar名称;
  4. Linux下执行chmod +x *.sh给sh脚本赋予执行权限;
  5. Linux下执行sh ./unZipJDK.sh解压JDK;
  6. Linux下执行sh ./start.sh
  7. Linux下执行sh tail -f nohup.......(省略)可查看控制台输出日志

注意:以下Linux脚本文件需要为Unix兼容格式(LF)
(区别:Windows默认CRLF回车换行符为\r\n,Unix的LF格式回车换行符为\n)

启动脚本start.sh

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
APP_NAME="application.jar"
APP_PORT=`sed -n '/^port/p' config/port.config | awk -F'=' '{print $2}'`
JMX_PORT=`sed -n '/^jmx-port/p' config/port.config | awk -F'=' '{print $2}'`
DUMP_DIR=$DIR/dump
if [ ! -d "${DUMP_DIR}" ]; then
    mkdir -vp ${DUMP_DIR}
fi

JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx1g -Duser.timezone=Asia/shanghai"
JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC"
JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${DUMP_DIR}/java_error_in_application_%p.hprof"
 
if [ ! "0$JMX_PORT" -gt "0" ]
then
    echo "没有指定 JMX 远程端口,将无法进行远程监控"
else
    echo "指定JMX端口为: $JMX_PORT"
    echo "尝试获取本机 ip 地址"
    IP_ADDR='127.0.0.1'
    IP_ADDR=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}' | head -1)
    echo "成功获取本机 ip 地址: $IP_ADDR"
    JAVA_OPT="${JAVA_OPT} -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=${JMX_PORT} -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=${IP_ADDR}"
fi
 
echo "程序运行参数为: ${JAVA_OPT}"
echo "程序运行端口为: ${APP_PORT}"
 
nohup jdk1.8.130/bin/java ${JAVA_OPT} -jar source/${APP_NAME} --server.port=${APP_PORT} > nohup_${APP_NAME}_${APP_PORT}.out 2>&1 &

停止脚本stop.sh

#!/bin/bash
APP_NAME="application.jar"
# 输入参数为要关闭Application的访问端口号
if [ -z "$1" ]
then
    pid=`ps ax |grep -i ${APP_NAME} |grep java | grep -v grep |  awk '{print $1}'`
else
    pid=`ps ax |grep -i ${APP_NAME} |grep java | grep -i 'server.port='''${1}''''| grep -v grep |  awk '{print $1}'`
fi
 
if [ -z "$pid" ] ; then
        echo "application is not running."
        exit 0;
fi
echo "application(${pid}) is running."
echo "Send shutdown request to application(${pid})....."
kill -9 ${pid}
echo "Shutdown application(${pid}) success."

解压JDK脚本unZipJDK.sh

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ ! -d $DIR/jdk1.8.311 ];then
    echo "正在解压jdk..."
    tar -zxf source/jdk-8u311-linux-x64.tar.gz -C $DIR
    echo "jdk解压完成"
else
    echo "已存在目录jdk1.8.311, 请勿重复解压JDK, 退出本脚本..."
fi

端口配置port.config

#app port
port=8080
#JMX port
jmx-port=

Windows脚本

使用说明:

  1. 控制台启动:双击run.bat文件
  2. 后台启动:命令行执行run.bat start
  3. 关闭程序:命令行执行run.bat stop

启停脚本run.bat

title Application(Java)

@echo off

rem ===========================================================

set JAVA_HOME_TEMP=source\jdk1.8.0_311

set JAVA_OPTS=-Xms4g -Xmx4g -Dfile.encoding=utf-8

set JAVA_JAR=source\application.jar

set PORT=9999

set LAUNCH_ARGS=--server.port=%PORT%

rem ===========================================================

set CLASSPATH_TEMP=".;%JAVA_HOME_TEMP%\bin;%JAVA_HOME_TEMP%\lib\dt.jar;%JAVA_HOME_TEMP%\lib\tools.jar"

rem 接收传入参数

if "%1"=="" goto doStartConsole

if "%1"=="start" goto doStart

if "%1"=="stop" goto doShutdown

:doStartConsole

set CMD=startConsole

goto doExe

:doStart

set CMD=start

goto doExe

:doShutdown

set CMD=stop

goto doExe

:doExe

echo ===========================================================

echo Java Home: %JAVA_HOME_TEMP%

echo JVM Parameters:%JAVA_OPTS:"=%

echo Start Jar: %JAVA_JAR:"=%

echo Start Parameters: %LAUNCH_ARGS:"=%

echo Execute Command: %CMD%

echo ===========================================================

echo.
if "%CMD%" == "startConsole" (
    %JAVA_HOME_TEMP%\bin\java %JAVA_OPTS% -classpath %CLASSPATH_TEMP% -server -jar %JAVA_JAR% %LAUNCH_ARGS%
) else if "%CMD%" == "start" (
    START %TITLE% /min %JAVA_HOME_TEMP%\bin\java %JAVA_OPTS% -classpath %CLASSPATH_TEMP% -server -jar %JAVA_JAR% %LAUNCH_ARGS%
) else if "%CMD%" == "stop" (
    for /f "tokens=1-5" %%a in ('netstat -ano ^| find ":%PORT%"') do (
        if "%%e%" == "" (
            set pid=%%d
        ) else (
            set pid=%%e
        )
        echo %pid%
        taskkill /f /pid %pid%
    )
)

goto end

:notParam

echo not params

:end

if "%CMD%" == "start" (
    echo 已后台启动!
)
if "%CMD%" == "stop" (
    echo 已停止!
)
if "%CMD%" == "startConsole" (
    pause
)
3

评论区