Computer Hope

Microsoft => Microsoft DOS => Topic started by: Blisk on June 28, 2017, 03:34:24 AM

Title: Batch for closing 2 instances of software
Post by: Blisk on June 28, 2017, 03:34:24 AM
Is it possible to close two instances of the same programs if found running in process, like two myprogram.exe.
If there is only one than leave it but if find two it will close both.
Is tha possible with batch?
Title: Re: Batch for closing 2 instances of software
Post by: Salmon Trout on June 28, 2017, 01:27:23 PM
You can use tasklist to count how many instances of a program are running. if that number is greater than 1 kill them all.

set nr=0
for /f "delims=" %%A in ('tasklist ^| find "notepad.exe"') do set /a nr+=1
if %nr% gtr 1 taskkill /F /im "notepad.exe"
Title: Re: Batch for closing 2 instances of software
Post by: Blisk on June 29, 2017, 01:26:46 AM
Thank you, exactly what I need.
Title: Re: Batch for closing 2 instances of software
Post by: Skylark on September 15, 2017, 10:26:56 AM
This kills all my instances of notepad it doesn't leave any up.
 :(
Title: Re: Batch for closing 2 instances of software
Post by: Salmon Trout on September 15, 2017, 10:40:59 AM
This kills all my instances of notepad it doesn't leave any up.

That's what you asked for.

Quote from: Skylark
Thank you, exactly what I need.


Title: Re: Batch for closing 2 instances of software
Post by: BC_Programmer on September 15, 2017, 12:26:58 PM
Apparently a different user who didn't read the OP original requirements.
Title: Re: Batch for closing 2 instances of software
Post by: Squashman on September 15, 2017, 12:54:20 PM
Apparently a different user who didn't read the OP original requirements.
This statement is pretty cut and dry in English English or American English.   ;D
if that number is greater than 1 kill them all.
Title: Re: Batch for closing 2 instances of software
Post by: Salmon Trout on September 15, 2017, 01:36:08 PM
Apparently a different user who didn't read the OP original requirements.
I didn't notice it was a different user. I did think that 2 and a half months was a bit long to decide it didn't do what they wanted.
Title: Re: Batch for closing 2 instances of software
Post by: DaveLembke on September 15, 2017, 05:29:57 PM
Currently have a similar issue with a product called Firestorm Viewer and I have been just manually going in to task manager and killing the extra instances of a executable that stays active when the viewer exits. I didnt catch this issue until opening and closing Firestorm Viewer about 5 times and then noticed some unexpected lag where the cores get weighed down with extra instances running but no window active for them etc. It seems as though this Viewer has a bug where it doesnt kill all threads on exit and starts to stack multiples. The biggest issue when killing the other instances is that if you select the wrong one you kill the currently active session vs the orphan remaining exe's that you want gone. So I just kill all of them now, and start fresh.

Killing all instances of the exe that acts up and then relaunching is the best method I found.  :)
Title: Re: Batch for closing 2 instances of software
Post by: Salmon Trout on September 16, 2017, 12:46:14 AM
If you used tasklist in a batch with the /v (verbose) and /fo:csv switches you could catch the window title in full

This is the header:

"Image Name","PID","Session Name","Session#","Mem Usage","Status","User Name","CPU Time","Window Title"

For example, this session of Firefox right now shows up:

"firefox.exe","3292","Console","1","711,312 K","Running","MIKEANDBARBIE2\Mike","0:08:51","Preview - Re: Batch for closing 2 instances of software - Mozilla Firefox"

You can use for /f with comma delim and catch the various tokens, e.g.
Code: [Select]
for /f "tokens=1-10 delims=," %%a in ('tasklist /v /FO:CSV ^| find /i "firefox"') do @echo PID %%b Win title %%j
Code: [Select]
PID "3292" Win title "Preview - Re: Batch for closing 2 instances of software - Mozilla Firefox"
You could present the PID (token 2) and window title (token 10) of each one to the user in a list and then invite them to type a PID to kill using taskkill
if each instance of Firestorm Viewer has a different window title that would work but if the orphaned threads have not got active windows I guess you'd need some other way to ID the ones you want to kill. I think the window title would show up as "N/A". However if the image name contained "Firestorm" maybe you could just nuke that PID.

Title: Re: Batch for closing 2 instances of software
Post by: DaveLembke on September 16, 2017, 07:40:10 AM
This here is the bug...

https://jira.phoenixviewer.com/browse/FIRE-17876

llceflib_host is the executable that starts to stack multiple instances. I have been just closing out the active window and going into task manager and sort alphabetical and kill all llceflib_host processes. Then relaunch firestorm viewer and its ok again. It only seems to happen when the viewer is exited and relaunched again as if there is a issue with garbage clean up at shutdown of services. No windows open with these services. I looked to see if there was a way to associate which one goes with the active window and which ones are orphan but its just pretty simple to close out the viewer and go to task manager sort alphabetical and kill all of the llceflib_host processes, then launch a fresh launch of firestorm.  :)