From e9d429b9b855e00744f5bab1863534bec95d32b2 Mon Sep 17 00:00:00 2001 From: henrygd Date: Mon, 28 Apr 2025 21:47:02 -0400 Subject: [PATCH] Enhance service start check in install-agent.ps1 - Added logic to handle service start failures and check status with retries. - Improved user feedback for service status during startup process. --- supplemental/scripts/install-agent.ps1 | 47 ++++++++++++++++++++------ 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/supplemental/scripts/install-agent.ps1 b/supplemental/scripts/install-agent.ps1 index 4b17840..a33ffe2 100644 --- a/supplemental/scripts/install-agent.ps1 +++ b/supplemental/scripts/install-agent.ps1 @@ -157,19 +157,44 @@ try { Write-Host "Starting beszel-agent service..." nssm start beszel-agent - if ($LASTEXITCODE -ne 0) { - throw "Failed to start beszel-agent service" - } + $startResult = $LASTEXITCODE - Write-Host "Checking beszel-agent service status..." - Start-Sleep -Seconds 5 # Allow time to start before checking status - $serviceStatus = nssm status beszel-agent - - if ($serviceStatus -eq "SERVICE_RUNNING") { - Write-Host "Success! The beszel-agent service is running properly." -ForegroundColor Green + # Only enter the status check loop if the NSSM start command failed + if ($startResult -ne 0) { + Write-Host "NSSM start command returned error code: $startResult" -ForegroundColor Yellow + Write-Host "This could be due to 'SERVICE_START_PENDING' state. Checking service status..." + + # Allow up to 20 seconds for the service to start, checking every 2 seconds + $maxWaitTime = 20 # seconds + $elapsedTime = 0 + $serviceStarted = $false + + while (-not $serviceStarted -and $elapsedTime -lt $maxWaitTime) { + $serviceStatus = nssm status beszel-agent + + if ($serviceStatus -eq "SERVICE_RUNNING") { + $serviceStarted = $true + Write-Host "Success! The beszel-agent service is now running properly." -ForegroundColor Green + } + elseif ($serviceStatus -like "*PENDING*") { + Write-Host "Service is still starting (status: $serviceStatus)... waiting" -ForegroundColor Yellow + Start-Sleep -Seconds 2 + $elapsedTime += 2 + } + else { + Write-Host "Warning: The service status is '$serviceStatus' instead of 'SERVICE_RUNNING'." -ForegroundColor Yellow + Write-Host "You may need to troubleshoot the service installation." -ForegroundColor Yellow + break + } + } + + if (-not $serviceStarted) { + Write-Host "Service did not reach running state within $maxWaitTime seconds." -ForegroundColor Yellow + Write-Host "You can check status manually with 'nssm status beszel-agent'" -ForegroundColor Yellow + } } else { - Write-Host "Warning: The service status is '$serviceStatus' instead of 'SERVICE_RUNNING'." -ForegroundColor Yellow - Write-Host "You may need to troubleshoot the service installation." -ForegroundColor Yellow + # NSSM start command was successful + Write-Host "Success! The beszel-agent service is running properly." -ForegroundColor Green } } catch {