场景1:java部署,Spring Cloud项目往往具备多个jar包,一条条执行指令需要时间,可以借助脚本【机器性能不好 就延长指令的执行间隔】
#!/bin/bash
# 设置 Java 运行参数,指定配置为 "prod",并添加Java虚拟机的参数
# 定义 JAR 包文件名数组
JAR_FILES=(
"api_gateway-0.0.1-SNAPSHOT.jar"
"service_acl-0.0.1-SNAPSHOT.jar"
"service_cms-0.0.1-SNAPSHOT.jar"
"service_edu-0.0.1-SNAPSHOT.jar"
"service_msm-0.0.1-SNAPSHOT.jar"
"service_order-0.0.1-SNAPSHOT.jar"
"service_oss-0.0.1-SNAPSHOT.jar"
"service_statistics-0.0.1-SNAPSHOT.jar"
"service_ucenter-0.0.1-SNAPSHOT.jar"
"service_vod-0.0.1-SNAPSHOT.jar"
)
# 循环运行每个 JAR 包
for JAR_FILE in "${JAR_FILES[@]}"
do
nohup java-Dspring.profiles.active=prod -Xms100m -Xmx100m -jar $JAR_FILE > "${JAR_FILE}.log" &
# 等待 30 秒
sleep 30
done
场景2:服务器上多个java进程快速关闭
#!/bin/bash
# 定义 JAR 包文件名数组
JAR_FILES=(
"api_gateway-0.0.1-SNAPSHOT.jar"
"service_acl-0.0.1-SNAPSHOT.jar"
"service_cms-0.0.1-SNAPSHOT.jar"
"service_edu-0.0.1-SNAPSHOT.jar"
"service_msm-0.0.1-SNAPSHOT.jar"
"service_order-0.0.1-SNAPSHOT.jar"
"service_oss-0.0.1-SNAPSHOT.jar"
"service_statistics-0.0.1-SNAPSHOT.jar"
"service_ucenter-0.0.1-SNAPSHOT.jar"
"service_vod-0.0.1-SNAPSHOT.jar"
)
# 停止每个 JAR 包的运行
for JAR_FILE in "${JAR_FILES[@]}"
do
# 查找正在运行的 Java 进程
PID=$(ps -ef | grep $JAR_FILE | grep -v grep | awk '{print $2}')
if [ -n "$PID" ]; then
echo "Stopping $JAR_FILE (PID: $PID)"
kill $PID
else
echo "$JAR_FILE is not running"
fi
done
场景3:windows服务器端口占用问题 ,不正常关闭服务或其他应用占用都会导致
【不一定有效 有的应用会自动重启再次占用 】
@echo off
rem 要关闭的端口列表
set PORTS=8222 8009 8004 8001 8005 8007 8002 8008 8160 8003
for %%p in (%PORTS%) do (
rem 查找占用指定端口的进程ID
for /f "tokens=5" %%a in ('netstat -aon ^| findstr /r "[ ]*:[ ]*%%p[ ]*.*LISTENING"') do (
set PID=%%a
)
if defined PID (
echo Port %%p is in use by process with PID: !PID!. Killing process...
rem 结束进程
taskkill /PID !PID! /F >nul 2>&1
) else (
echo Port %%p is not in use.
)
)