Linux和Windows系统使用脚本操作jar包

1、Windows系统

Windows脚本文件一般命名: 文件.bat

1.1 启动jar包脚本

在Windows系统上面创建 start.bat 启动jar包脚本编辑以下内容

@echo off
java -jar test-security-1.0.jar -Dspring.profiles.active=test --server.port=9999 > test.log 2>&1 &

rem @echo off 包括 echo off 在内以下的脚本内容都不回显
rem test-security-1.0.jar jar包
rem -Dspring.profiles.active=test 环境
rem --server.port=9999 端口
rem test.log 日志

1.2 停止jar包脚本

在Windows系统上面创建 stop.bat 停止jar包脚本编辑以下内容

@echo off
set port=9999
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (
    echo kill the process %%m who use the port 
    taskkill /pid %%m -t -f
    goto start
)
:start
rem Windows自动查询指定端口号并杀进程
rem set port=9999 设置端口
rem 通过for循环遍历这个端口并杀死他的进程
rem 可以发现,上面的命令基本上都是Windows系统里面的命令
rem 'netstat -ano^|findstr ":%port%"' 查看进程
rem taskkill /pid %%m -t -f 杀死进程

1.3 重新启动jar包脚本

重启jar包就是将两个合并到一起

@echo off
set port=9999
for /f "tokens=1-5" %%i in ('netstat -ano^|findstr ":%port%"') do (
    echo kill the process %%m who use the port 
    taskkill /pid %%m -t -f
    goto start
)
:start

%1 mshta vbscript:CreateObject("WScript.Shell").Run("%~s0 ::",0,FALSE)(window.close)&&exit
java -jar test-security-1.0.jar -Dspring.profiles.active=test --server.port=9999 > test.log 2>&1 &
exit

2、Linux系统

Linux系统本质上启动jar包和Windows大致相同

在Linux系统上面创建 start.sh启动jar包脚本编辑以下内容

2.1 启动jar包脚本

#!/bin/bash
nohup java -jar test-security-1.0.jar -Dspring.profiles.active=test --server.port=9081 > test.log 2>&1 &
# linux脚本的开头都有一行注释,例如: #!/bin/bash或者#!/bin/sh,这行注释的作用就是声明解析当前文件要使用的解释器。通常我们写的脚本都是包含各种系统命令,来实现定制功能的,所以都是使用bash和sh解释器的。

2.2 停止jar包脚本

在Linux系统上面创建 stop.sh启动jar包脚本编辑以下内容

#!/bin/bash
PID=$(ps -ef | grep test-security-1.0.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo Application is already stopped
else
    echo kill $PID
    kill $PID
fi

2.3 重启jar包脚本

#!/bin/bash
PID=$(ps -ef | grep test-security-1.0.jar | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo Application is already stopped
else
    echo kill $PID
    kill $PID
fi
    
nohup java -jar test-security-1.0.jar -Dspring.profiles.active=test --server.port=9081 > test.log 2>&1 &

猜你喜欢

转载自blog.csdn.net/ITKidKid/article/details/130779070