Mario Lopez
Mario Lopez

Reputation: 1

Shell script to kill process by usage?

Is there a shell script i can run that can kill a process or application being run on a server that exceeds a certain thresh hold of either memory or CPU usage? I'd be running this via an RMM that would detect the condition to be true for a certain amount of time and then would execute this script once the condition was met.

Haven't been able to find anything on windows, have seen similar scripts for linux but nothing thus far for powershell.

Upvotes: 0

Views: 174

Answers (1)

Vern_Anderson
Vern_Anderson

Reputation: 100

You could do this but I'm not sure it's a solution for prduction environments

do
{
    Start-Sleep -Seconds 03
}
until ((Get-Process notepad | Select-Object -ExpandProperty WS) -gt 67098880)
Stop-Process -Name notepad

This will do it based on "workingset" WS which is memory based you could do it on CPU as well or whetever your criteria is.

What is the goal you want to acheive?

<# You could also schedule a windows task schedule to run a powershell command every 5 minutes something like. . . #>

Get-Process -Name chrome | Where-Object {$.CPU -gt 200} | Foreach-Object {Stop-Process -Id $.Id}

<# If your filtering is not setup correctly this could have undesired results and killing processes seems like a band aid to a much bigger issue with the product #>

Upvotes: 0

Related Questions