Skip to content

Pre Stop & Post Stop Actions

Akram El Assas edited this page Jun 13, 2026 · 10 revisions

Table of Contents

  1. Introduction
  2. Shutdown Hook Lifecycle
  3. Pre-Stop
    1. Pre-Stop Settings
    2. GUI Example
    3. CLI Example
    4. PowerShell Example
  4. Post-Stop
    1. Post-Stop Settings
    2. GUI Example
    3. CLI Example
    4. PowerShell Example

Introduction

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.

Shutdown Hook Lifecycle

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

Pre-Stop Settings

  • 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.

GUI Example

servy-config-pre-stop

CLI Example

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" `
  --preStopLogAsError

PowerShell Example

Import-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

Post-Stop Settings

  • 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.

GUI Example

servy-config-post-stop

CLI Example

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"

PowerShell Example

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 @installParams

Clone this wiki locally