业务需求:1,windows上执行脚本 2,先检查是否运行着A.jar 3,如果有运行着的A.jar,则结束该任务,再重新运行A.jar,并修改进程名; 4,如果没有运行着A.jar,则直接运行A.jar并修改进程名 5,并检查运行状态和结束状态是否正常 请给一个windows脚本
时间: 2024-03-15 13:45:53 浏览: 110
shell脚本:一键启动当前目录所有jar包、一键停止jar包、一键查看当前目录所有jar包的进程数、一键重启jar包脚本
以下是一个可能的Windows脚本,可以满足您的需求:
```
@echo off
set PROCESS_NAME=A.jar
set NEW_PROCESS_NAME=A_modified.jar
REM 检查是否有运行着的A.jar
tasklist /FI "IMAGENAME eq %PROCESS_NAME%" | findstr /i %PROCESS_NAME% >nul
if %errorlevel% neq 1 (
echo A.jar is running. Stopping the task...
REM 结束该任务
taskkill /f /im %PROCESS_NAME%
REM 等待一段时间,确保进程已经结束
timeout /t 5 /nobreak >nul
)
REM 运行A.jar并修改进程名
echo Starting A.jar...
start "" "%~dp0%PROCESS_NAME%" && (
timeout /t 5 /nobreak >nul
wmic process where name="%PROCESS_NAME%" CALL setpriority "high priority"
wmic process where name="%PROCESS_NAME%" CALL setpriority "idle"
wmic process where name="%PROCESS_NAME%" CALL setpriority "high priority"
wmic process where name="%PROCESS_NAME%" CALL setpriority "idle"
wmic process where name="%PROCESS_NAME%" CALL setpriority "high priority"
wmic process where name="%PROCESS_NAME%" CALL setpriority "idle"
wmic process where name="%PROCESS_NAME%" CALL setpriority "high priority"
wmic process where name="%PROCESS_NAME%" CALL setpriority "idle"
)
REM 修改进程名
echo Modifying process name...
for /f "tokens=2 delims=," %%a in ('wmic process where "name='%PROCESS_NAME%'" get processid^,status /format:csv') do (
set status=%%a
goto :next
)
:next
if "%status%"=="Running" (
for /f "tokens=2 delims=," %%a in ('wmic process where "name='%PROCESS_NAME%'" get processid^,executablepath /format:csv') do (
set path=%%a
goto :rename
)
:rename
set newpath=%path:%PROCESS_NAME%=%NEW_PROCESS_NAME%
wmic process where "name='%PROCESS_NAME%'" CALL update "%newpath%"
)
REM 检查运行状态和结束状态是否正常
echo Checking status...
timeout /t 5 /nobreak >nul
tasklist /FI "IMAGENAME eq %NEW_PROCESS_NAME%" | findstr /i %NEW_PROCESS_NAME% >nul
if %errorlevel% neq 0 (
echo Failed to start A.jar.
pause
exit /b 1
)
echo Checking exit status...
timeout /t 5 /nobreak >nul
tasklist /FI "IMAGENAME eq %NEW_PROCESS_NAME%" | findstr /i %NEW_PROCESS_NAME% >nul
if %errorlevel% equ 0 (
echo A.jar is still running.
pause
exit /b 1
)
echo A.jar has exited successfully.
pause
exit /b 0
```
请注意,此脚本仅供参考,并且未经过完整测试。在实际使用中,请根据您的具体需求进行修改,并确保脚本的正确性和安全性。
阅读全文