Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch for closing 2 instances of software  (Read 4838 times)

0 Members and 1 Guest are viewing this topic.

Blisk

    Topic Starter


    Intermediate

    Thanked: 1
    • Experience: Familiar
    • OS: Windows 7
    Batch for closing 2 instances of software
    « 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?

    Salmon Trout

    • Guest
    Re: Batch for closing 2 instances of software
    « Reply #1 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"

    Blisk

      Topic Starter


      Intermediate

      Thanked: 1
      • Experience: Familiar
      • OS: Windows 7
      Re: Batch for closing 2 instances of software
      « Reply #2 on: June 29, 2017, 01:26:46 AM »
      Thank you, exactly what I need.

      Skylark



        Starter

        • Experience: Experienced
        • OS: Windows 7
        Re: Batch for closing 2 instances of software
        « Reply #3 on: September 15, 2017, 10:26:56 AM »
        This kills all my instances of notepad it doesn't leave any up.
         :(

        Salmon Trout

        • Guest
        Re: Batch for closing 2 instances of software
        « Reply #4 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.



        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Batch for closing 2 instances of software
        « Reply #5 on: September 15, 2017, 12:26:58 PM »
        Apparently a different user who didn't read the OP original requirements.
        I was trying to dereference Null Pointers before it was cool.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Batch for closing 2 instances of software
        « Reply #6 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.

        Salmon Trout

        • Guest
        Re: Batch for closing 2 instances of software
        « Reply #7 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.

        DaveLembke



          Sage
        • Thanked: 662
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Batch for closing 2 instances of software
        « Reply #8 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.  :)

        Salmon Trout

        • Guest
        Re: Batch for closing 2 instances of software
        « Reply #9 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.


        DaveLembke



          Sage
        • Thanked: 662
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: Batch for closing 2 instances of software
        « Reply #10 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.  :)