Is there a way to only kill cmd.exe processes pertaining to a specific task schedule?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Is there a way to only kill cmd.exe processes pertaining to a specific task schedule?

#1 Post by SIMMS7400 » 12 Apr 2017 18:12

Is there a way to only kill cmd.exe processes executed by a specific task schedule, rather than ALL when running:

Code: Select all

taskkill /f /im cmd.exe


Thanks!!

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Is there a way to only kill cmd.exe processes pertaining to a specific task schedule?

#2 Post by ShadowThief » 12 Apr 2017 18:33

You want to get the PIDs of all existing CMD windows before the task is run and compare it to the list of all CMD windows after the task is run. I could swear I wrote something similar once a while ago, but it may have been in Powershell...

Unless, of course, the cmd window opened by the task has a unique custom title, in which case you can easily identify and close the window without making any comparisons to previous windows.
Last edited by ShadowThief on 12 Apr 2017 18:34, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Is there a way to only kill cmd.exe processes pertaining to a specific task schedule?

#3 Post by Squashman » 12 Apr 2017 18:34

Did you look at any of the filter options?

Code: Select all

Filters:
    Filter Name   Valid Operators           Valid Value(s)
    -----------   ---------------           -------------------------
    STATUS        eq, ne                    RUNNING |
                                            NOT RESPONDING | UNKNOWN
    IMAGENAME     eq, ne                    Image name
    PID           eq, ne, gt, lt, ge, le    PID value
    SESSION       eq, ne, gt, lt, ge, le    Session number.
    CPUTIME       eq, ne, gt, lt, ge, le    CPU time in the format
                                            of hh:mm:ss.
                                            hh - hours,
                                            mm - minutes, ss - seconds
    MEMUSAGE      eq, ne, gt, lt, ge, le    Memory usage in KB
    USERNAME      eq, ne                    User name in [domain\]user
                                            format
    MODULES       eq, ne                    DLL name
    SERVICES      eq, ne                    Service name
    WINDOWTITLE   eq, ne                    Window title

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Is there a way to only kill cmd.exe processes pertaining to a specific task schedule?

#4 Post by SIMMS7400 » 13 Apr 2017 14:45

Squashman wrote:Did you look at any of the filter options?

Code: Select all

Filters:
    Filter Name   Valid Operators           Valid Value(s)
    -----------   ---------------           -------------------------
    STATUS        eq, ne                    RUNNING |
                                            NOT RESPONDING | UNKNOWN
    IMAGENAME     eq, ne                    Image name
    PID           eq, ne, gt, lt, ge, le    PID value
    SESSION       eq, ne, gt, lt, ge, le    Session number.
    CPUTIME       eq, ne, gt, lt, ge, le    CPU time in the format
                                            of hh:mm:ss.
                                            hh - hours,
                                            mm - minutes, ss - seconds
    MEMUSAGE      eq, ne, gt, lt, ge, le    Memory usage in KB
    USERNAME      eq, ne                    User name in [domain\]user
                                            format
    MODULES       eq, ne                    DLL name
    SERVICES      eq, ne                    Service name
    WINDOWTITLE   eq, ne                    Window title



HI Squash -

Thanks for the note! So, I originally went that route, but having an issue. For some reason, when a batch is launched by Task Schedule, it doesn't assume a title. How come?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Is there a way to only kill cmd.exe processes pertaining to a specific task schedule?

#5 Post by Squashman » 13 Apr 2017 16:08

SIMMS7400 wrote:HI Squash -

Thanks for the note! So, I originally went that route, but having an issue. For some reason, when a batch is launched by Task Schedule, it doesn't assume a title. How come?

Probably because you have the scheduled task set as Run whether user is logged on or not. This will run the task in the background without a Window. Hence no Window Title.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Is there a way to only kill cmd.exe processes pertaining to a specific task schedule?

#6 Post by SIMMS7400 » 13 Apr 2017 16:38

Squashman wrote:
SIMMS7400 wrote:HI Squash -

Thanks for the note! So, I originally went that route, but having an issue. For some reason, when a batch is launched by Task Schedule, it doesn't assume a title. How come?

Probably because you have the scheduled task set as Run whether user is logged on or not. This will run the task in the background without a Window. Hence no Window Title.



Hmm thank you Squash! This makes thing tricky now. I'd prefer not to have a cmd window pop up but I also need a way to kill a specific cmd session and I was using a title and a unique identifier (hour) to do so. then the taskkill found the cmd with the specified title and the PREVIOUS hour and killed that one, leaVing the current session..back to the drawing board I go

Post Reply