简体   繁体   中英

Batch script to close desired Command Prompt windows

First, I have a script that starts 4 java programs:

@echo off
cd "C:\work\clientes\loremIpsum\loremIpsum\loremIpsum\target"
start "BROKER" java -jar loremIpsum-broker.jar

cd "C:\work\clientes\loremIpsum\loremIpsum\loremIpsum-virtual-devices-engine\target"
start "VIRTUAL-DEVICES-ENGINE" java -jar loremIpsum-virtual-devices-engine.jar

cd "C:\work\clientes\loremIpsum\loremIpsum\loremIpsum-websocket\target"
start "WEBSOCKET" java -jar loremIpsum-websocket.jar

cd "C:\work\clientes\loremIpsum\loremIpsum-loremIpsum\dandelion-workflow-engine\target"
start "WORKFLOW-ENGINE" java -jar loremIpsum-workflow-engine.jar
exit

I want to make a script that closes only these 4. I´ve tried with wmic:

@echo off
wmic process where "name like '%java%'" delete
exit

But I don't want it to close my other java aplications, only these 4

The most likely unique and known commandline, would be its ending, eg -broker.jar . So the wildcard, % should represent the beginning of the commandline, ie %-broker.jar , (doubled in a batch file %%-broker.jar ) . You would therefore use the following:

WMIC Process Where "CommandLine Like '%%-broker.jar'" Call Terminate

I would suspect however that java.exe actually passes the initial command through a separate instance of cmd.exe , opening it in another window, which could be problematic, unless we knew what command it passed!

If the java command actually opens in a new cmd.exe window, with the window title you've stipulated, BROKER , you should be able to terminate it using TaskKill . Perhaps something like this:

TaskKill /F /FI "WindowTitle Eq BROKER" /T >Nul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM