Page 1 of 2
Kill Windows Process
Posted: Thu Sep 28, 2017 11:43 am
by JohnFLi
I have tried searching for this, but maybe I'm not searching correctly.
What I would like to do is this:
if a windows process (not service) gets above 95% of cpu.....kill it
any ideas on where to look for that??
Re: Kill Windows Process
Posted: Thu Sep 28, 2017 12:05 pm
by dwasswa
Hi
@JohnFLi,
Do you wish to kill the process locally or remotely?
Re: Kill Windows Process
Posted: Thu Sep 28, 2017 12:09 pm
by JohnFLi
either way is fine.
I'm thinking a powershell script on the machine itself would be easiest.
In trying to figure this out myself i've been testing with the process "notepad.exe"
Re: Kill Windows Process
Posted: Thu Sep 28, 2017 12:21 pm
by dwasswa
Once you have the name of the process a simple tskill is all it takes:
Go to cmd.exe windows and run
please make sure you dont add
it will give you this error
Code: Select all
could not find process:notepad.exe
For powershell run command:
Code: Select all
Get-Process notepad | Stop-Process
Please let me know if you have any questions.
Re: Kill Windows Process
Posted: Thu Sep 28, 2017 12:22 pm
by JohnFLi
but I need to check to see if it's cpu% has surpassed 95% before killing it
Re: Kill Windows Process
Posted: Thu Sep 28, 2017 1:03 pm
by mcapra
Hmm, the work would be non-trivial. It would almost certainly involve an event handler assigned to some "CPU Usage" service check under the Windows host though.
So lets say I have a really basic service check of CPU usage against the Windows host. It screams when CPU Usage is above 95% and does absolutely nothing else. No process information, just screams when CPU Usage >95%. This establishes the "problem" criteria we want to react to.
Once the problem criteria is well understood, we can write an event handler to take action. But how does the event handler know which process it's supposed to kill? That's really the tricky part; Figuring out how to bake that knowledge into your event handler or the remote script the Windows machine runs to kill the process.
If it were me, I'd script out some Powershell to grab the top X high usage processes (via WMI or something) for the host machine and kill the top one on that list. Or kill only those processes with >95% CPU Usage; There's ways to do either. Then I'd configure it as a command in NSClient++ and call that command as my event handler for the "CPU Usage" check.
Re: Kill Windows Process
Posted: Thu Sep 28, 2017 1:34 pm
by dwasswa
Re: Kill Windows Process
Posted: Thu Sep 28, 2017 1:56 pm
by JohnFLi
the issue i am currently having is getting the CPU for a specific process.
Once that process is using over 95% cpu, then I can kill it.
if I run (in powershell) Get-Process | Sort CPU -descending | Select -first 1 -Property ID,CPU,ProcessName
it will tell me what process is teh highest cpu usage.
But then checking to see if the processName is (the process i'm looking for) and over 95 then i could simplely kill it as I have teh ID
basiclly, needing an IF/Then syntax
Re: Kill Windows Process
Posted: Fri Sep 29, 2017 10:04 am
by kyang
Hey
@JohnFLi,
Did you have any more questions? It sounds like you are on the right track.
Re: Kill Windows Process
Posted: Fri Sep 29, 2017 10:29 am
by mcapra
This should get you all processes with >95% CPU:
Code: Select all
Get-WmiObject Win32_PerfFormattedData_PerfProc_Process `
| Where-Object { $_.Name -inotmatch '_total|idle' -and $_.PercentProcessorTime > 95} `
| ForEach-Object {
"Process={0,-25} CPU_Usage={1,-12}" -f `
$_.Name,$_.PercentProcessorTime
}
I would think you could replace what's currently in the
ForEach-Object block with a step to kill the matching processes. Either by
Name or by
IDProcess.
All available fields for that WMI object:
https://technet.microsoft.com/en-us/aa394277(v=vs.90)