Skip to content

Pre Launch & Post Launch Actions

Akram El Assas edited this page Jul 24, 2026 · 12 revisions

Table of Contents

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

Introduction

Servy supports running an optional script or executable before the main service starts. This can be used for tasks like:

  • Preparing or generating configuration files
  • Fetching secrets or credentials dynamically
  • Running any setup or initialization required before the main process

Additionally, Servy supports running an optional script or executable after the process starts successfully. This can be used for tasks such as:

  • Initializing dependent services or processes
  • Running database migrations or setup scripts
  • Sending notifications or logging startup events
  • Performing environment-specific configuration

Note

If the pre-launch (or post-launch) 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:

--preLaunchPath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
--preLaunchParams="-File C:\Scripts\GenerateConfig.ps1 -VaultUrl https://vault.example.com"

Directly specifying --preLaunchPath="C:\Scripts\GenerateConfig.ps1" will not work.

Startup Hook Lifecycle

Hook Type Mode Trigger Failure Impact Orphan Cleanup
Pre-Launch Synchronous Timeout > 0 Service won't start Yes
Pre-Launch Fire-and-Forget Timeout = 0 Logged, service starts Yes
Post-Launch Fire-and-Forget Default Logged, service stays up Yes

For shutdown-related hooks, see Pre-Stop & Post-Stop Actions.

Servy tracks processes launched during startup to ensure that if the main service crashes, all setup scripts are also terminated.

Pre-Launch

Pre-Launch Settings

  • Pre-Launch Executable Path – full path to the pre-launch script or executable
  • Pre-Launch Startup Directory – working directory for the pre-launch process (optional; defaults to the main service's working directory)
  • Pre-Launch Parameters – command-line arguments for the pre-launch executable
  • Pre-Launch Environment Variables – optional environment variables for the pre-launch process (same format as main process)
  • Pre-Launch Stdout/Stderr File Paths – log files for capturing output and errors
  • Pre-Launch Timeout (seconds) – maximum allowed time per attempt, set to 0 to run in fire-and-forget mode (default: 30 seconds)
  • Pre-Launch Retry Attempts – number of retries on failure (default: 0)
  • Ignore Failure – if enabled, service startup proceeds even if pre-launch script fails

The pre-launch script is executed synchronously. If it fails or times out (and Ignore Failure is disabled), the main service will not start.

Setting the timeout to 0 runs the pre-launch hook in fire-and-forget mode. When set to 0, the hook is started and the service is launched immediately without waiting for completion. Use this only for tasks that do not affect the service's ability to start or run correctly. stdout/stderr redirection and retries are not available in fire-and-forget mode.

Fire-and-forget pre-launch hooks are executed as part of the start sequence and are tracked for orphan cleanup when the service stops.

GUI Example

servy-config-pre-launch

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" `
  --preLaunchPath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
  --preLaunchStartupDir="C:\Scripts" `
  --preLaunchParams="-File C:\Scripts\GenerateConfig.ps1 -VaultUrl https://vault.example.com" `
  --preLaunchEnv="ENV=production;API_KEY=abcdef123" `
  --preLaunchStdout="C:\Logs\prelaunch_stdout.log" `
  --preLaunchStderr="C:\Logs\prelaunch_stderr.log" `
  --preLaunchTimeout="60" `
  --preLaunchRetryAttempts="2" `
  --preLaunchIgnoreFailure

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"

    PreLaunchPath            = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    PreLaunchStartupDir      = "C:\Scripts"
    PreLaunchParams          = "-File C:\Scripts\GenerateConfig.ps1 -VaultUrl https://vault.example.com"
    PreLaunchEnv             = "ENV=production;API_KEY=abcdef123"
    PreLaunchStdout          = "C:\Logs\prelaunch_stdout.log"
    PreLaunchStderr          = "C:\Logs\prelaunch_stderr.log"
    PreLaunchTimeout         = 60
    PreLaunchRetryAttempts   = 2
    PreLaunchIgnoreFailure   = $true
}

Install-ServyService @installParams

Post-Launch

Post-Launch Settings

  • Post-Launch Executable Path – full path to the post-launch script or executable
  • Post-Launch Startup Directory – working directory for the post-launch process (optional; defaults to the main service's working directory)
  • Post-Launch Parameters – command-line arguments for the post-launch 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-launch script is executed after the service process has fully started in fire-and-forget mode.

Post-launch hooks run after the service has started and are tracked for orphan cleanup when the service stops.

GUI Example

servy-config-post-launch

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" `
  --postLaunchPath="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
  --postLaunchStartupDir="C:\Scripts" `
  --postLaunchParams="-File C:\Scripts\Notify.ps1 -VaultUrl https://vault.example.com"

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"

    PostLaunchPath         = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    PostLaunchStartupDir   = "C:\Scripts"
    PostLaunchParams       = "-File C:\Scripts\Notify.ps1 -VaultUrl https://vault.example.com"
}

Install-ServyService @installParams

Clone this wiki locally