question

MohammadGanji-0581 avatar image
0 Votes"
MohammadGanji-0581 asked MohammadGanji-0581 answered

Killing processes with no CPU usage in a specific time

Hi,
There is a bug in an application that should be remediated but for now, this is what I'm going to do:

Problem:
Instances (processes) with the same name are produced (for example, worker.exe)
The users create these instances to do their job and then leave the app but the instance is not killed by the app and remains running, doesn't free up the memory while there is no CPU usage. Therefore, you can see many worker.exe processes with 0% CPU usage and hundreds of megabytes of RAM used by each of them which leads to high memory utilization on the server.

What I need:
I'd like to write a script to monitor these processes and kill them if they're not using CPU and have been present for, let's say 15 minutes, to make a workaround temporarily. I hope I've made myself clear here. BTW, O.S. is Windows server 2016 and 2019.

Regards,
M. Ganji



windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @MohammadGanji-0581 ,

just to get a better understanding off your requirement:
If the process is consuming CPU within the 15 minutes just for a very short time (a short CPU peak) the "15 minutes period" needs to be restarted?
Or should this kind of short CPU usage peak be ignored?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·
MohammadGanji-0581 avatar image
0 Votes"
MohammadGanji-0581 answered

Thanks, Andrea, At the first glance I'm noticing some differences between this and what I need. But, I'll check it more and share my feedback.

Regards,

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @MohammadGanji-0581 ,

maybe this is helpful?

https://gist.github.com/Badgerati/fcc89d73b9546f5c675adaee209581a3


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

You may try the counter "\Process(chrome)\% Processor Time" although it's somewhat different.

 Get-Counter -Counter "\Process(chrome)\% Processor Time" | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue

For more details you may refer to this link
https://social.technet.microsoft.com/wiki/contents/articles/12984.understanding-processor-processor-time-and-process-processor-time.aspx

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MohammadGanji-0581 avatar image
0 Votes"
MohammadGanji-0581 answered

Thanks, Rich,
I think I've already elaborated on what I want. If that is not easy to get, well, that's another thing.

To make it easier, I'd like to check processes for a 15-minute period with 1s intervals. If they never get above 1% CPU usage, they're candidates to be killed. Is this possible?

Or let's break it into small steps. How can I get live CPU usage of a process (as observed in performance monitor of task manager in Windows) via Powershell?

Thanks

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

This isn't as easy as you might think.

Each process has a "Responding" property, but all processes that have a "MainWindowHandle" (that isn't a zero) have a window and all processes that have a window always report that they're "Responding". So unless your application is a console application that's useless.

If you're looking for "zombie" or ("orphaned") processes (ones that have a ParentProcessID) but the parent process has ended, then that's usually pretty easy: for each process, the ParentProcessID should exist as a process. You can't use Get-Process for this, but Get-WMIObject or Get-CIMInstance will report the ParentProcessID (using the Win32_Process class).

But if you're looking for "idle" processes you'd have to keep track of each process (or maybe just ones with "ProcessName" that's the same as you application -- minus the file extension). You'd also have to separate the "user" time and "system" time. If both are non-varying between checks then they might be candidates to be killed. You may also be able to check the owner of each process and if the owner is no longer present on the server the process may be a candidate for termination. You'd have to update the information and add new processes and remove processes that have ended normally.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MohammadGanji-0581 avatar image
0 Votes"
MohammadGanji-0581 answered

Hi,
It won't.
The process starts using CPU and when done the RAM is not freed and the process uses about 500GB of RAM but the CPU remains 0%. So, any process which is using 0% CPU for 15 minutes can be killed. If it even uses 1% CPU the 15 minutes timer should be restarted but I'm nearly sure that when it goes to 0% it will remain 0% for eternity.

Regards,

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.