Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
    
which services cause the high CPU usage? Use xperf to see which functions cause the usage. –  magicandre1981 Apr 1 '13 at 6:01
    
We 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. –  Gh0sT Apr 1 '13 at 9:17
    
Yeah ... 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 powershell Get-WmiObject win32_processor | select LoadPercentage |fl which will give you CPU usage and to restart services use command sc with proper switches/parameters. –  mnmnc Apr 1 '13 at 9:46
    
It'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. –  Gh0sT Apr 1 '13 at 12:43
2  
you should contact the creator of the tools and ask them why the service hogs the CPU. –  magicandre1981 Apr 1 '13 at 18:44

2 Answers 2

up vote 1 down vote accepted

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!

share|improve this answer

If a commercial solution is OK, check out our Service Protector. It will babysit any windows service and restart it if it hogs the CPU for too long.

share|improve this answer
    
As I mentioned already...these are NOT windows services –  Gh0sT Apr 1 '13 at 12:58
    
@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? –  mnmnc Apr 1 '13 at 13:09
    
Yes..exactly..these are third party services –  Gh0sT Apr 1 '13 at 13:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.