2

I have multiple processes by the same name (say multiple FIREFOX.exe process). A Batch script then opens another firefox.exe process and, after some predefined time, needs to kill the process it opened.

I had the following solution for it

start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -P "ABC" -no-remote
PING 127.0.0.1 -n 10 -w 1000 >NUL
for /F "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq firefox.exe"') do set MyPID=%%b
taskkill /PID %MyPID% /T 

This works fine EXCEPT if another process by the same name (firefox.exe) opens after the 1st command (i.e running firefox), the taskkill command actually kills the latest process instead of the one it started

So how do i make sure the script only kills the process it started and none other.

1 Answer 1

1

Anyway i solved the issue myself.

Actually, the solution above itself needs a small tweak. Thats all

start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -P "ABC" -no-remote
for /F "TOKENS=1,2,*" %%a in ('tasklist /FI "IMAGENAME eq firefox.exe"') do set MyPID=%%b    
PING 127.0.0.1 -n 10 -w 1000 >NUL
taskkill /PID %MyPID% /T 

What was earlier happening was i was setting the %MyPID% after the PING command (which is just a time delay). So in that period, any new process( with same name) which opened, got its PID registered for taskkill. I just simply moved the command immediately after the process opening command and now it works.

Technically, though, if there are too many same processes opening (say within milliseconds), by multiple scripts, this can still cause issues and taskill can kill wrong process (May be someone can write a answer about that. i will not accept my answer as of now...but will wait for a day or 2, for that) :)

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .