Shell script that monitors CPU usage and terminates services with high CPU utilisation
Just for the record..here's the answer:
$cpuutil=(get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 |
select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
If ($cpuutil -ge 90)
{Restart-Service MyService1, "My Service2", MyService3}
Else
{Exit}
Powershell truly makes life simpler!
Related videos on Youtube
Gh0sT
Updated on September 18, 2022Comments
-
Gh0sT over 1 year
We have a Windows 2003 server where 3 services are running constantly. Sometimes these services consume over 90% CPU. Restarting these services returns normalcy. I need a script/program that will constantly monitor CPU usage and if usage is high then restart those services.
After a bit of research I found this script to monitor CPU usage from Technet.
Script to monitor CPU usage:
(get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 10 | select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
This monitors the CPU usage for 10 seconds and then displays the averaged output.
Also from Stack Overflow and Server Fault I found the scripts to restart Windows services. (Which one is better?)
Now all I need is for the CPU usage script to call the service restart scripts when the condition that usage is >90% is met. Any help would be appreciated.
-
magicandre1981 about 11 yearswhich services cause the high CPU usage? Use xperf to see which functions cause the usage.
-
Gh0sT about 11 yearsWe run an EPABX application on the server..the services are related to the same...I know the services..I only need a way out to restart them automatically when them automatically when they start hogging resources.
-
mnmnc about 11 yearsYeah ... would be great if yo could specify what kind of Operating system are do you need it for. The solution for Linux is really easy -
mpstat -A | grep all | head -1
will give you CPU usage and based on its value you could restart the services. In windows i would advise to use powershellGet-WmiObject win32_processor | select LoadPercentage |fl
which will give you CPU usage and to restart services use commandsc
with proper switches/parameters. -
Gh0sT about 11 yearsIt's Server 2003. @mnmnc I think that WMI script you worte would show the CPU usage at any particular instant. Now there can be a number of reasons that the CPU usage can suddenly spike. I specifically need a script that will monitor the usage for a period of time (say 10 seconds) then if it's >90%, will restart some specific services. I might add that the services don't stop instantly...so we need to wait some time/check their status to start them again. Any help would be highly appreciated. Thanks in advance.
-
magicandre1981 about 11 yearsyou should contact the creator of the tools and ask them why the service hogs the CPU.
-
gronostaj about 11 yearsIf such a thing happens then these services are poorly programmed, you should contact developers to fix them before more serious problems start to pop up.
-
-
Gh0sT about 11 yearsAs I mentioned already...these are NOT windows services
-
mnmnc about 11 years@gh0sT - I'm confused - you've said that the OS is Win2003 server and now that those are not Windows services. I guess you might mean that the services are not created by Microsoft as in 'not shipped with Operating System' but a third party services running on Windows2003 server. Am I correct?
-
Gh0sT about 11 yearsYes..exactly..these are third party services