-
-
Notifications
You must be signed in to change notification settings - Fork 81
Shutdown & Teardown
Servy v6.2 introduces high-reliability teardown during OS shutdown/reboot, mimicking the behavior of robust wrappers like NSSM.
-
Pre-Shutdown Registration: Intercepts
SERVICE_CONTROL_PRESHUTDOWNto start cleanup before the standard shutdown phase begins. - SCM Heartbeats: Periodically reports progress (Wait Hints and Checkpoints) to the Windows Service Control Manager (SCM) to prevent "forced termination."
-
Synchronized Teardown: Built-in protection against race conditions if
StopandShutdownsignals arrive simultaneously.
IMPORTANT: You must reinstall your service using the Servy v6.2+ executable. Older installations do not have the SERVICE_ACCEPT_PRESHUTDOWN flag registered in the Windows SCM database.
By default, Windows provides a grace period for services to stop during shutdown. If your service and all associated hooks complete within 30 seconds, no action is needed.
If your service requires more time, you must manually increase the global OS limits. Note that the Service Control Manager (SCM) reads these values only at boot, so a reboot is required after applying these changes.
The following PowerShell script can be used to extend the shutdown timeout. Run it as Administrator, set your desired timeout in milliseconds, and then reboot the system.
# Registry path
$registryPath = "HKLM:\SYSTEM\CurrentControlSet\Control"
# Timeout in milliseconds (example: 80 seconds)
$timeoutValue = 80000
# 1. Set PreshutdownTimeout (DWORD, milliseconds)
if (Get-ItemProperty -Path $registryPath -Name "PreshutdownTimeout" -ErrorAction SilentlyContinue) {
Set-ItemProperty -Path $registryPath -Name "PreshutdownTimeout" -Value $timeoutValue
} else {
New-ItemProperty -Path $registryPath -Name "PreshutdownTimeout" -Value $timeoutValue -PropertyType DWord -Force
}
# 2. Set WaitToKillServiceTimeout (REG_SZ, milliseconds)
Set-ItemProperty -Path $registryPath -Name "WaitToKillServiceTimeout" -Value "$timeoutValue"
Write-Host "Shutdown timeouts updated to $timeoutValue ms."
Write-Host "A system reboot is required for these changes to take effect."Notes:
-
PreshutdownTimeoutspecifies the maximum time the OS waits for services registered for pre-shutdown. -
WaitToKillServiceTimeoutcontrols how long the OS waits for all services to stop before forcing termination. - Adjust
$timeoutValueaccording to the maximum time your service needs to stop cleanly.
Copyright © Akram El Assas. All rights reserved.
- Home
- Overview
- Installation Guide
- Advanced Configuration
- Usage
- Servy Desktop App
- Servy Manager
- Servy CLI
- PowerShell Module
- Examples & Recipes
- Logging & Log Rotation
- Health Monitoring & Recovery
- Environment Variables
- Service Dependencies
- Pre-Launch & Post-Launch Actions
- Pre-Stop & Post-Stop Actions
- Shutdown & Teardown
- Export/Import Services
- Automation & CI/CD
- Integration with Monitoring Tools
- Service Event Notifications
- Comparison with Alternatives
- Security
- Architecture
- Building from Source
- Troubleshooting
- FAQ