-
-
Notifications
You must be signed in to change notification settings - Fork 81
Pre Stop & Post Stop Actions
Servy supports running an optional script or executable before the main service stops. This can be used for tasks such as:
- Gracefully shutting down external dependencies
- Flushing logs or metrics
- Notifying external systems before shutdown
Servy also supports running an optional script or executable after the service process has stopped. This can be used for tasks such as:
- Cleanup operations
- Notifications or alerts
- Post-shutdown automation
Note
If the pre-stop (or post-stop) script is a PowerShell script (.ps1) or any non-executable file, it must be invoked through an executable such as powershell.exe or pwsh.exe.
For example:
--preStopPath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
--preStopParams="-File C:\Scripts\PreStop.ps1 -VaultUrl https://vault.example.com"Directly specifying --preStopPath="C:\Scripts\PreStop.ps1" will not work.
| Hook Type | Mode | Trigger | Failure Impact | Orphan Cleanup |
|---|---|---|---|---|
| Pre-Stop | Synchronous | Timeout > 0 | Logged, service stops | No |
| Pre-Stop | Fire-and-Forget | Timeout = 0 | Logged, service stops | No |
| Post-Stop | Fire-and-Forget | Default | Logged | No |
For startup-related hooks, see Pre-Launch & Post-Launch Actions.
Servy tracks processes launched during startup to ensure that if the main service crashes, all setup scripts are also terminated. For Stop hooks, cleanup is disabled because these processes are intended to finish their work as the service environment itself is being torn down.
- Pre-Stop Executable Path: Full path to the pre-stop script or executable
- Pre-Stop Startup Directory: Working directory for the pre-stop process (optional; defaults to the main service's working directory)
- Pre-Stop Parameters: Command line arguments for the pre-stop executable
- Pre-Stop Timeout (seconds): Maximum allowed execution time. Default is 5 seconds. Set to 0 to run in fire-and-forget mode
- Log Pre-Stop Failure as Error: When enabled, pre-stop failures are logged as errors
Environment Variables: This hook inherits the main service's EnvironmentVariables. There is no separate Post-Launch / Pre-Stop / Post-Stop / Failure-Program EnvironmentVariables setting - only Pre-Launch supports a per-hook override.
The pre-stop script is executed synchronously by default. Servy waits for the script to complete before continuing the service shutdown process.
If the script fails or times out and Log Pre-Stop Failure as Error is enabled, the failure is logged as an error and the service continues stopping.
Setting the timeout to 0 runs the pre-stop hook in fire-and-forget mode. In this mode, the hook is started and the service shutdown continues immediately without waiting for completion. This should only be used for non-critical tasks.
Fire-and-forget pre-stop hooks are executed as part of the stop sequence and are not tracked for orphan cleanup.
servy-cli install `
--name="MyService" `
--description="Runs app with dynamic config" `
--path="C:\Apps\App\App.exe" `
--startupDir="C:\Apps\App" `
--params="--mode=production" `
--preStopPath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
--preStopStartupDir="C:\Scripts" `
--preStopParams="-File C:\Scripts\PreStop.ps1 -VaultUrl https://vault.example.com -SecretName AppSecrets" `
--preStopTimeout="60" `
--preStopLogAsErrorImport-Module "C:\Program Files\Servy\Servy.psm1" -Force
$installParams = @{
Name = "MyService"
Description = "Runs app with dynamic config"
Path = "C:\Apps\App\App.exe"
StartupDir = "C:\Apps\App"
Params = "--mode=production"
PreStopPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
PreStopStartupDir = "C:\Scripts"
PreStopParams = "-File C:\Scripts\PreStop.ps1 -VaultUrl https://vault.example.com"
PreStopTimeout = 60
PreStopLogAsError = $true
}
Install-ServyService @installParams- Post-Stop Executable Path: Full path to the post-stop script or executable
- Post-Stop Startup Directory: Working directory for the post-stop process (optional; defaults to the main service's working directory)
- Post-Stop Parameters: Command line arguments for the post-stop executable
Environment Variables: This hook inherits the main service's EnvironmentVariables. There is no separate Post-Launch / Pre-Stop / Post-Stop / Failure-Program EnvironmentVariables setting - only Pre-Launch supports a per-hook override.
The post-stop script is executed after the service process has fully stopped in fire-and-forget mode.
Post-stop hooks run after the service has stopped and are not tracked for orphan cleanup.
servy-cli install `
--name="MyService" `
--description="Runs app with dynamic config" `
--path="C:\Apps\App\App.exe" `
--startupDir="C:\Apps\App" `
--params="--mode=production" `
--postStopPath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
--postStopStartupDir="C:\Scripts" `
--postStopParams="-File C:\Scripts\Notify.ps1 -VaultUrl https://vault.example.com -SecretName AppSecrets"Import-Module "C:\Program Files\Servy\Servy.psm1" -Force
$installParams = @{
Quiet = $true
Name = "MyService"
Description = "Runs app with dynamic config"
Path = "C:\Apps\App\App.exe"
StartupDir = "C:\Apps\App"
Params = "--mode=production"
PostStopPath = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
PostStopStartupDir = "C:\Scripts"
PostStopParams = "-File C:\Scripts\Notify.ps1 -VaultUrl https://vault.example.com"
}
Install-ServyService @installParamsCopyright © 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