Linux and Windows systems use scripts to operate jar packages

1. Windows system

Windows script files are generally named: file.bat

1.1 Start the jar package script

Create start.bat on the Windows system to start the jar package script and edit the following content

@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 Stop the jar package script

Create stop.bat on the Windows system to stop the jar package script and edit the following content

@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 Restart the jar package script

Restarting the jar package is to merge the two together

@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 system

The essence of the Linux system to start the jar package is roughly the same as that of Windows

Create the start.sh start jar package script on the Linux system and edit the following content

2.1 Start the jar package script

#!/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 Stop the jar package script

Create a stop.sh startup jar package script on the Linux system and edit the following content

#!/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 Restart the jar package script

#!/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 &

Guess you like

Origin blog.csdn.net/ITKidKid/article/details/130779070