Monitoring a sevice using while loop

Arosh Wijepala 161 Reputation points
2022-05-19T03:30:56.983+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. SChalakov 10,261 Reputation points MVP
    2022-05-19T05:51:13.2+00:00

    Hi @Arosh Wijepala ,

    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

    0 comments No comments

  2. Newbie Jones 1,306 Reputation points
    2022-05-19T11:31:52.453+00:00

    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.

    0 comments No comments