What is PID advantage? - pid

For example, to kill all java processes in background, I found two ways to do it by batch script. One is using PID, and the other one is not. What's the differences between these two methods, and what's the advantage using PID?
Without PID:
taskkill /F /IM java.exe
With PID:
FOR /F "usebackq tokens=2 skip=2" %%i IN (`TASKLIST /FI "IMAGENAME eq java.exe"`) DO taskkill /F /PID %%i

They are equivalent so you should just use the first version. taskkill with /IM matches the image name. In the second case, you are manually building a list of PIDs matching the same image name, and then killing them one-by-one in a loop. You would use the PID version when you need more control over the specific processes being killed instead of just everything with a particular image name.

Related

Need to separate multiple tokens into multiple parameters that can then be set to separate variables

Disclaimer: I have absolutely no coding experience in any language.
I need to create a script that can find the PID for a process, of which there will be multiple running, and then create a process dump for each PID. Procdump doesn't allow the process name to be used if more then one is running, so my goal is to to have the PID found for the process and then be set to variables for later commands.
Here is a .bat I have made:
for /F "tokens=2" %%K in ('
tasklist /FI "ImageName eq T.exe" /FI "Status eq Running" /FO LIST ^| findstr /B "PID:"
') do (
set "X= %%K"
)
c:\Users\Public\Documents\Procdump\procdump.exe -ma %X%
pause
I am able to get the last PID set and used in a command, but I can't figure out how to get the first two.
This may seem like a lot of work for one go, but the later goal is for this to run every 2 and 5 hours for 24 hours. I have been able to get that to work so for now i just need to solve the PID problem. In hindsight, maybe powershell would have been a better choice, but i feel like i got close so I want to to see this route through.
There could be three processes max. I was thinking it would look something like this:
for /F "tokens=2" %%K in ('
tasklist /FI "ImageName eq T.exe" /FI "Status eq Running" /FO LIST ^| findstr /B "PID:"
') do (
set "X= %%K"
set "Y=???"
set "Z=???"
)
c:\Users\Public\Documents\Procdump\procdump.exe -ma %X%
c:\Users\Public\Documents\Procdump\procdump.exe -ma %Y%
c:\Users\Public\Documents\Procdump\procdump.exe -ma %Z%
pause
If you need to execute procdump.exe nice per item, simply move it into the for /F loop with -ma %%K as the parameters

Trying to kill a CMD with a title with a BAT script

Used a lot of time to se if i could figure this out but no luck.
My issue currently is via CMD i can do
taskkill /F /FI "WindowTitle eq Administrator: Server2" /T
But i add this to a bat file, it removs the extra space between Administrator: Server2 and for some reason that wont work ofc.
The thing i wanna do is being able to close down a specific cmd with a bat script, since that CMD runs a game server, and i dont want to target them all ad once.
Is there any way to only close the specific CMD?
Since "WMIC process" doesn't include "windowtitle", I reverted back 'tasklist' and 'findstr /l /c:"your string goes here"'.
The "/l" and "/c" together allow you to specify extra spaces. Although I don't have an example on my system with the same number of spaces your example has, I recall using for something similar in the past. Please try this and let me know:
Cmd
for /f "tokens=2" %A in ('tasklist /v /fi "imagename eq chrome.exe" ^|findstr /l /c:"Administrator: Server2"') do #echo taskkill /pid %A

Taskkill: Do I need to specify \t parameter multiple times?

I have 2 unwanted processes running: foo.exe and bar.exe, and both have child processes started by them.
I want to use taskkill to terminate all these processes (foo.exe, bar.exe and all child ones). Do I need to use \t parameter only once or do I need to use it multiple times? I.e., which version is correct:
a) taskkill /f /im "foo.exe" /im "bar.exe" /t
b) taskkill /f /im "foo.exe" /t /im "bar.exe" /t
?
Well, a for loop will be more meaningful as you can add/remove imagenames as you please:
Using batch file:
#echo off
for %%i in (foo.exe bar.exe) do taskkill /f /im "%%i" /t
From cmdline:
for %i in (foo.exe bar.exe) do taskkill /f /im "%i" /t

How can i kill all the processes with same name and save one with same name but using exact PID?

How can i kill all the processes with name "OPCExplorer.exe" except the one process with name "OPCExplorer.exe" but for example with PID = "8888" ?
Use the filters of the tasklist command:
#echo off
for /f "skip=3 tokens=2 delims= " %%a in ('tasklist /fi "imagename eq OPCExplorer.exe" /fi "PID ne 8888"') do (
taskkill /pid %%a /f
)
You could use the built-in WMIC.exe
WMIC Process Where "Name='OPCExplorer.exe' And Not ProcessId='8888'" Call Terminate

BATCH,How to search for an specific cmd process(from multiple cmd processes) and kill that specific process [duplicate]

I want to write a simple batch file to kill a process that contains certain text in the window title. Right now I have:
taskkill /fi "Windowtitle eq XXXX*" /im cmd.exe
And that works, except what I want to do is use the wildcard both at the beginning AND end of the title. So something like:
taskkill /fi "Windowtitle eq \*X*" /im cmd.exe
But I tried this and it does not work. Is there something I'm missing or is this not possible?
No, wildcards are not allowed at the start of the filter.
for /f "tokens=2 delims=," %%a in ('
tasklist /fi "imagename eq cmd.exe" /v /fo:csv /nh
^| findstr /r /c:".*X[^,]*$"
') do taskkill /pid %%a
This will retrieve the list of tasks, in csv and verbose format (that will include window title as the last field in the output).
The list is filtered by findstr with a regular expression that will search the indicated text (the X) in the last field.
If any line matches the filter, the for will tokenize it, retrieving the second field (the PID) that will be used in taskkill to end the process.
In the special case you have started the command window from a batch file yourself, you can specify the window title using the command
START MyWindowTitle c:/MyProcess.exe
That way it is easy to kill the process again using just
taskkill /fi "WindowTitle eq MyWindowTitle"
A little more elaborate, but you can use:
for /f "tokens=2 delims== " %%A in ('tasklist /nh /fi "imagename eq cmd.exe" /fi "windowtitle eq MyWindowTi*"') do set "PID=%%A"
taskkill /F /T /PID !PID!

Resources