Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop-service cmdlet timeout possible?

We're using the stop-service cmdlet to kill a few services on our boxes. Most of the time it works great, however we have one or two services (who doesn't?) that occasionally don't play nice.

In this instance one of the services in question will remain in the stopping state, and the cmdlet puts this out to the console over and over:

[08:49:21]WARNING: Waiting for service 'MisbehavingService (MisbehavingService)' to finish 
[08:49:21]stopping...
[08:49:23]WARNING: Waiting for service 'MisbehavingService (MisbehavingService)' to finish 
[08:49:23]stopping... 
[08:49:25]WARNING: Waiting for service 'MisbehavingService (MisbehavingService)' to finish 
[08:49:25]stopping...

Eventually we have to kill the service in the task manager, and our script then continues.

Is there a way to have the stop-service cmdlet give up or timeout after a certain point? I figure we can check afterward and if the service is still running, use the kill-process cmdlet to provide a final chop.

like image 924
larryq Avatar asked Sep 21 '12 16:09

larryq


People also ask

How do I restart a service in PowerShell?

Start-Service (Microsoft.PowerShell.Management) - PowerShell The Start-Service cmdlet sends a start message to the Windows Service Controller for each of the specified services. If a service is already running, the message is ignored without error.

Does PowerShell have a timeout?

ps1 Timeout is for 10 seconds Waiting for 2 seconds, press CTRL+C to quit ... If you enter the Ctrl+C, it will terminate the entire script execution.

How do I restart a PowerShell service remotely?

Method 3: Using PowerShellGet-Service -ComputerName computername -Name servicename | Restart-Service -Force. Get-Service -ComputerName computername -Name servicename | Stop-Service -Force. Get-Service -ComputerName computername -Name servicename | Start-Service.


1 Answers

Although Stop-Service does not have a timeout parameter, the WaitForStatus method on the System.ServiceController class does have an overload that takes a timeout parameter (documented here). Fortunately, this is exactly the type of object that the Get-Service command returns.

Here is a simple function that takes a service name and a timeout in seconds. It returns $true if the service stops before the timeout is reached, and $false if the call times out (or if the service isn't present).

function Stop-ServiceWithTimeout ([string] $name, [int] $timeoutSeconds) {
    $timespan = New-Object -TypeName System.Timespan -ArgumentList 0,0,$timeoutSeconds
    $svc = Get-Service -Name $name
    if ($svc -eq $null) { return $false }
    if ($svc.Status -eq [ServiceProcess.ServiceControllerStatus]::Stopped) { return $true }
    $svc.Stop()
    try {
        $svc.WaitForStatus([ServiceProcess.ServiceControllerStatus]::Stopped, $timespan)
    }
    catch [ServiceProcess.TimeoutException] {
        Write-Verbose "Timeout stopping service $($svc.Name)"
        return $false
    }
    return $true
}
like image 126
JamesQMurphy Avatar answered Oct 05 '22 22:10

JamesQMurphy