Sunday, April 26, 2009

End (kill) some application from command line

Different methods exist in Windows to end or kill some application from command line.

The normal way to do this is by using the TASKKILL command, for example to end FireFox one can use:

TASKKILL /F /IM “firefox.exe”


When you need to stop some command window it can be dangerous to just kill any cmd.exe, so one could use the window title to kill a specific batch window, for example to kill any batch window with name starting with “SIM-” I used:



TASKKILL /FI "WINDOWTITLE eq SIM-*"


But sometimes you’ll need to be more specific, I wanted an automated way to kill some Java application but only when it was started with some specific parameter. The only way I found to do so on Windows was by using the WMIC commands. This example show how I killed our Java monitor application which started as follows:



java -Dapp.prop.file.monitor="monitor_system.properties" -classpath SOME_JARS Monitor


Kill this specific Java instance:



wmic process where "name like '%%java.exe%%' AND commandline like '%%monitor%%'" DELETE


Before killing your application, you want to make sure your query will result in the correct process, one can easily check the query by executing the similar wmic command without yet killing the process:



wmic process where "name like '%%java.exe%%' AND commandline like '%%monitor%%'" get processid, name, commandline /VALUE


More examples and combinations that can be used with wmic are available on this page.



The Linux equivalent we used was pkill, for example to kill our simulators we used:



pkill -f "simulator.jar"

No comments:

Post a Comment