I basically need to achieve two things,
- Find all "firefox.exe" processes running on a windows server
- Kill the ones that have been running longer than 30 mins
I have bits and pieces, but not sure how to integrate all of it to make it working as a windows service.
What I have so far -
1) Way to find all running firefox process
wmic process get name,creationdate, processid | findstr firefox
2) Way to kill a process based on PID
taskkill /PID 827
What else is left?
- Calculate based on
creationdate
, whichPID
is running for longer than 30 mins - Use the
taskkill
command to sequentially kill all the PIDs that fit the above criteria - set this is a service (this I can probably figure out)
I believe you're doing some sort of test automation and there are a lot of hanging firefoxes, so in order to kill ALL those who run more than half and hour, do:
It's actually what we're doing.
It's easy to think, "You can't do that in .bat". I know that was my first reaction. The problem is you need date manipulation which is not directly supported and is non-trivial. But then Ritchie Lawrence comes to the rescue, having done all the hard work of writing the necessary date functions.
The WMI
Process
class providesCreationDate
in UTC format.Win32_OperatingSystem
LocalDateTime
gives us the current time in UTC format. We need to subtract the maximum lifetime (30 minutes in your case) from LocalDataTime to get the CutOffTime. Then we can use that to filterProcess
, and finallycall terminate
(instead oftaskkill
). Rather thanfindstr
, I use a WMIwhere
filter (which is much faster).The following code seems to work. As this is KILLING TASKS, you should test this for yourself.
Note:
if "%%p" GEQ "0"
is used to filter out the 'blank' line at the end of the results of wmic which is not empty but contains a newline character. As we're expecting a number, this seemed a simple and effective test (though maybe there's a better way to handle this).Using Powershell
The original question was tagged with "batch-file" so I gave the Windows bat answer first. But I wanted to mention killing a named process that has been running longer than 30 minutes is trivial to do with Powershell:
Again this is KILLING TASKS so please be careful. If you're not familiar with Powershell here's the breakdown of the one liner:
Get-Process
cmdlet to get the processes that are running on the local computer. Here the name of the process,firefox
is passed in. If needed a list of names can be passed, and wildcards can be used.Get-Process
are piped toWhere
to filter on the StartTime property. TheGet-Date
cmdlet is used to get the current date and time as aDateTime
type. The AddMinutes method is called to add -30 minutes, returning aDateTime
that represents 30 minutes ago. The-lt
operator (technically, in this context, it's a switch parameter), specifies the Less-than operation.Where
are piped to theStop-Process
cmdlet. The-Force
parameter is used to prevent the confirmation prompt.Above I've used the simplified
Where
syntax introduced in Powershell 3. If you require Powershell 2 compatibility (which is the latest version that runs on Windows XP), then this syntax is required:Here the curly braces enclose a ScriptBlock.
$_
is used to explicitly reference the current object.