question

phantom2000-5377 avatar image
0 Votes"
phantom2000-5377 asked NewbieJones-6218 answered

Monitoring a sevice using while loop

Hello,

I'm using the below code to continuously monitor a service and do some work once it's detected to be stopped.

 $SvcName = "VMnetDHCP"
 While ($True) 
 {
     if ((Get-Service -Name $SvcName).Status -eq 'Stopped')
     {
         Write-Host "Service is stopped"
         #Do something here when service is detected to be stopped. 
         Start-Sleep -Seconds 60
    
     }
    
     if ((Get-Service -Name $SvcName).Status -eq 'Running')
     {
         Write-Host "Service is running"
           
     }
 }

But I feel like this may be a resource hog and bad practice to run the script in a while true loop. But I can't think of anyother way to monitor this service. Any advise would be greatly appreciated.

Thanks

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

StoyanChalakov avatar image
0 Votes"
StoyanChalakov answered

Hi @phantom2000-5377,

The "while" statement is pretty basic, so you know what you get. Since you are not doing advanced operations and calculations in your script, there should be absoolutely no issues with a simple service status check. You are not filling up the error stack neither, so this is also fine.

You can of course do a simple test and llet it run for a while, while monitoring yoour CPU and memory utilization, but I am sure you won't see any big movement there.

Hope I was able to help.


(If the reply was helpful please don't forget to upvote or accept as answer, thank you)
Regards,
Stoyan

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

NewbieJones-6218 avatar image
0 Votes"
NewbieJones-6218 answered

Does it really have to be continuously monitored, or can you just run a script every 5 minutes via a scheduler like the Windows Task Scheduler.

If you have access to Azure Application Insights, you could also record the result there which will give you an availability map over time of said service.

Decent monitoring tools will re-run the test on failure a number of times before finally reporting as a failure.
This is normally configurable and based on the importance of what is being monitored. Perhaps make this into a function with this as a configuration option. (number of retries).
This can prevent false\positives and other connectivity issues, but might not be appropriate based on the service you are monitoring.

Finally, have you considered investing in a monitoring tool like PRTG or an SNMP tool. (Windows services can be monitored by SNMPc and you can usually try a force restart, just make sure you change the default password if you install the agent on a server). Most of these reporting tools are not real time, but poll the service\system\server on a configurable time element. 2 minutes, five minutes, ten minutes, etc. The tools can then perform various actions on failures which you can probably replicate in PowerShell, but expect to be writing a fair bit of code to implement this properly, with error handling, etc.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.