Link to home
Start Free TrialLog in
Avatar of Craig Paulsen
Craig PaulsenFlag for New Zealand

asked on

Scripting HELP

Experts,
I require some assistance with the following:

I am working on a script that detects if the CPU usage is high on the server, then stop/starts the specific offending service (attempts to at least)
Can you advise what I need to add to the script in order to restart the server if a particular (or offending service) doesn't start correctly?

This is what I've got thus far

@Echo OFF
REM Craig Paulsen - Restart WDS Service Beta 1.1


SET    "SERVICE=WDSServer"
SET /A "MAXUSAGE=60"
SET /A "INTERVAL=300"

:LOOP
For /F %%P in ('wmic cpu get loadpercentage ^| FINDSTR "[0-9]"') do (
    IF %%P GTR %MAXUSAGE% (
        Echo [%TIME:~0,8%] CPU Usage: %%P%% Reached the limit: %MAXUSAGE%%%
        Echo Stopping %SERVICE% ...
         SC STOP  "%SERVICE%" 1>NUL
      timeout /t 1500
        SC START "%SERVICE%" 1>NUL
        Echo Attempt %SERVICE% restart.
    ) ELSE (
         Echo [%TIME:~0,8%] CPU Usage: %%P%%
    )
)
Ping -n %INTERVAL% Localhost >NUL
GOTO :LOOP


Your help will be most appreciated.

Regards,
Craig
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

The restart command is simple - shutdown -r - f (-r = reboot, -f = force)
Avatar of Craig Paulsen

ASKER

I agree Lee, it's simple enough, however as per my question

""what do I need to add to the script in order to restart the server if a particular (or offending service) doesn't start correctly?""

So im wanting it to attempt to restart a particular service, from our monitoring, we know this service "sometimes" doesn't start gracefully, and the only way to restore it is to restart the server, im trying to get that SMARTS built into the above mentioned script
You can use perfmon.

Setup perfmon performance monitoring. It let's you configure actions (scripts', whatever you like) and send alerts. If I need to elaborate on that, please make sure to look at perfmon first.
thanks McKnife, can you expand on that please, I've had a look at PerfMon but can't quite work out how I will achieve what I'm wanting....
ASKER CERTIFIED SOLUTION
Avatar of McKnife
McKnife
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks McKnike,
thanks,