bat checks the number of a certain process and kills it if it exceeds the limit

Scenario: The developed program, when multiple users use the downloaded certificate at the same time, many winword.exe processes will be generated (it is not automatically released, there are more than 20), which causes the subsequent failure to download the certificate. So I thought of this method

for /F %%i in ( 'tasklist ^| findstr winword.exe ^| find /c /v "" ') do ( set commitid=%%i)


set  /a limitNum=10
set /a winwordNum=%commitid%

echo %winwordNum%


if "%winwordNum%" gtr "%limitNum%" (
taskkill /f /im winword.exe
)

::winword.exe超过10个,则将其全部杀死
::使用任务计划程序,每隔1分钟调用本bat脚本

Among the pits stepped on:

1. Assign the result after the execution statement of the batch. It is recommended to use for to operate and assign the variable to the variable.

2. The | symbol behind tasklist must be changed to ^| escape, otherwise a syntax error will be reported.

3. If you write a wrong grammar, even if you add pause at the end, it will flash by after double-clicking to execute it.

4. When comparing if, add double quotes, and then use %var% to compare with variables. I didn't understand why I used variables and pure numbers before, and I didn't execute the statements in the if body.

Reference materials:

https://www.cnblogs.com/zndxall/p/9188300.html

Guess you like

Origin blog.csdn.net/Nightwish5/article/details/109193208