Batch script to close all open Command Prompt windows - windows-7

I have a .cmd file which I call to open multiple instances of Command Prompt via:
launcher.cmd -fs
launcher.cmd -tds
launcher.cmd -fsd
Each command open a new command prompt.
So what I want to do is create a batch file to automatically close all the opened Command Prompt instead of manually doing it.

Be carefull: you might kill more processes than you want:
taskkill /IM cmd.exe
You can add extra filters:
taskkill /IM cmd.exe /FI "WINDOWTITLE eq launcher*"
use
tasklist /FI "imagename eq cmd.exe " /V
to get a glimpse of what cmd.exe processes will be taskkill-ed
You could add the /F parameter to force the process to close but I would only use that if the process doesn't respond to a normal request.

Just a little note why accepted answer from Rene may not work. I was starting my apps from cmd file like
start "" my.exe -my -args
where my.exe was a console app and it was looking like cmd window I wanted to kill, but process name was not cmd.exe (!) and I had to use command like
taskkill /IM my.exe
So in some cases it worth to check the real process name, for example in the windows task manager.

TASKKILL /F /IM cmd.exe /T
good solution

Related

How can I stop one instance via cmd of a process when several are running?

I start a program from a scilab script via the command line, start myprog.exe.
After the start my scilab script needs to keep going.
Now I want to stop exactly this instance of the process via the command line too, even if several instances of the same program are running.
Is that possible?
I know how to query via batch files whether a process of this program is running and then stop it, but I don't know how to get the exact allocation.
Is there something like a process id?
I use this to check if the process is running:
tasklist /fi "imagename eq ccx.exe" |find ":" > nul
if errorlevel 1 echo Program is running
if not errorlevel 1 echo Program is not running
Use the command tasklist to view all running tasks with their PID
then
Taskkill /PID 26356 /F
or
Taskkill /IM myprog.exe /F

TASKKILL specific Python script

I have a working TASKKILL command that kills python.exe using Process Name
I'd like to narrow the scope of the command to kill a specific process (myScript.py) but can't use ProcessID as it changes with every run.
Is there a way I can add detail from the Command Line which knows the python script's name?
Current Command:
Taskkill /IM python.exe /F >nul 2>&1
if errorlevel 1 (echo PYTHON.exe NOT FOUND) else (echo PYTHON.exe KILLED)
You might be able to kill it based on your Python script's memory usage. In my case, the Python script is running a GUI, so the size gives it away.
taskkill /f /fi "IMAGENAME eq python.exe" /fi "MEMUSAGE gt 130000"
This reads as, forcefully kill the task (taskkill /f) identified by (/fi) the Python executable (IMAGENAME eq python.exe) which is using more than 130,000KB (MEMUSAGE gt 130000)1.
1 See taskkill /? for builtin help.
N.B. You might find this SO post helpful: Find Windows PID of a python script with Windows Command Prompt. Unfortunately, for me, It Doesn't Work™ but maybe you will have better luck.

How to kill a specific task in windows 2012 server

This is for windows 2012 server. I am trying to kill a specific task running under cmd window.
In this specific case I want to kill SWIMAUX. I have a command prompt open and every time I use taskkill command I get the following error. How can I kill that task?
C:\Siemens\hot_cold_backups>taskkill /f /fi "imagename eq SWIMAUX"
INFO: No tasks running with the specified criteria.
C:\Siemens\hot_cold_backups>taskkill /f /fi "services eq SWIMAUX"
INFO: No tasks running with the specified criteria.
You need to either specify the image name for the process you want to kill or the window title.
If the window has a specific title, then you can use the WINDOWTITLE parameter of the taskkill command
TASKKILL /F /FI "WINDOWTITLE eq SWIMAUX"
To find the image name of a process:
Open task manager and right click on the process you want to kill, and select Go to details
Find the highlighted executable and use it in your taskkill command, eg. taskkill /f /t /im process.exe
step 1:netstat -ano | findstr :
You should be known your PORT number
Step 2:taskkill /PID /F
id will be shown in the end of the TCP connection data in the result of above command

Cannot kill a process using the window title

I have two batch file one is always running (listerner.bat) but it is visible,the other one (mystop.bat)is to kill or stop my listener.bat and this two batch file are resides in this path C:\mydemo\mybatchfiles\,I am using windows 7
here is the code for mystop.bat
taskkill /F /FI "WINDOWTITLE eq Administrator: testlistener" /T
but when I run it,it will not terminate the running (listener.bat),There is no error but I have this message when I run it.
INFO: No tasks running with the specified criteria.
I appreciate someone can help me on this.I am new on this batch file command.
I had the same problem. In my case it was that there were two spaces in the window title:
taskkill.exe /F /FI "WindowTitle eq Administrator: TailingErrorLog"
^^
Taskkill considers the command currently executed by listener.bat as part of the title. So you need to add a wildcard "*".
taskkill /F /FI "WINDOWTITLE eq Administrator: testlistener *" /T
Try terminating using Im switch ..
Taskkill /Im listener.bat /t /f
Although this is not computer science related question , thought I should help ...

how to kill all batch files except the one currently running

How can you run a batch file which taskkills all other cmd.exes which are currently running, except for the one that is doing the task kill command?
Combining these 2 threads:
how to get PID from command line filtered by username and imagename
how to get own process pid from the command prompt in windows
you can write down something like this in a simple cmd file (akillfile.cmd)
title=dontkillme
FOR /F "tokens=2 delims= " %%A IN ('TASKLIST /FI ^"WINDOWTITLE eq dontkillme^" /NH') DO SET tid=%%A
echo %tid%
taskkill /F /IM cmd.exe /FI ^"PID ne %tid%^"
copy cmd.exe, rename it to a.exe, then use this command in a batch file: start a.exe /k taskkill /f /im cmd.exe
This worked for me, added to the very beginning of the .bat which is to kill all previously launched instances of itself, then immediately make itself susceptible to be killed by subsequent calls of the same .bat file:
title NewlyLaunchedThing
taskkill /F /IM cmd.exe /FI "WINDOWTITLE ne NewlyLaunchedThing"
title Thing
...do everything else
Note that, apparently, the "title" is a keyword, which sets a variable named "WINDOWTITLE".
Also, the "=" (equals sign) is apparently optional for assigning title/WINDOWTITLE. Meaning, this works as well, and may be preferred for clarity:
title = newTitle

Resources