Skip to content

Environment Variables

Akram El Assas edited this page Feb 8, 2026 · 29 revisions

Table of Contents

  1. Introduction
  2. Environment Variable Expansion
  3. GUI
  4. CLI Option: --env
  5. CLI Example
  6. PowerShell Option: -Env
  7. PowerShell Example
  8. Tips

Introduction

Servy allows you to define environment variables for the service process, enabling fine-grained control over the runtime context.

Environment variable expansion is supported in multiple areas of Servy configuration:

  • Service binary paths (--path / -Path)
  • Startup directories (--startupDir / -StartupDir)
  • Process parameters (--params / -Params)
  • Environment variables (--env / -Env)

This makes it possible to reference existing system variables, user-defined variables, or paths dynamically in your configuration.

Environment Variable Expansion

Servy uses a sophisticated expansion engine that allows variables to reference each other. The resolution follows this specific order:

  1. System Environment: All current system and process environment variables are loaded first.
  2. Custom Overrides: Variables defined in the Environment Variables field (or --env) are added next. If a custom variable has the same name as a system variable, the custom value wins.
  3. Cross-Reference Expansion: Finally, all variables are scanned for %VAR% placeholders. These placeholders are resolved using the final merged set of variables.

Example of Expansion Logic: If your system has TEMP=C:\Windows\Temp and you define:

  • MY_ROOT=C:\ServyApp
  • MY_LOGS=%MY_ROOT%\logs
  • APP_TEMP=%TEMP%

The final environment seen by your process will be:

  • MY_ROOT: C:\ServyApp
  • MY_LOGS: C:\ServyApp\logs
  • APP_TEMP: C:\Windows\Temp

Note

Variable Ordering and Circular References: To ensure reliable expansion when custom variables reference one another, define the "parent" variable (e.g., MY_ROOT) before the "child" variable (e.g., MY_LOGS) in your configuration list. Avoid circular references (for example, setting A=%B% and B=%A%), as these cannot be resolved and may result in the literal placeholder string remaining in your environment.

GUI

The advanced tab in Servy allows setting environment variables for the service process:

servy-config-advanced-env

CLI Option: --env

The --env command-line option lets you specify environment variables for the service process.

  • Syntax: --env="VAR1=value1; VAR2=value2"
  • Separate multiple variables with semicolons (;)
  • Special characters can be escaped:
    • \= to escape =
    • \" to escape "
    • \; to escape ;
    • \\ to escape \
  • Supports environment variable expansion. Example:
    --env="VAR1=%ProgramData%\MyApp; VAR2=%VAR1%\bin"
  • Useful for setting runtime context without changing system-wide environment variables.

CLI Example

servy-cli install `
  --name="My NodeJS Service" `
  --description="My NodeJS Server" `
  --path="%ProgramFiles%\nodejs\node.exe" `
  --startupDir="C:\Apps\App" `
  --params="C:\Apps\App\index.js" `
  --startupType="Automatic" `
  --env="NODE_ENV=production; APP_CONFIG=C:\Apps\App\config.json"

PowerShell Option: -Env

The -Env parameter lets you define environment variables when installing a service via PowerShell.

  • Type: string (semicolon-separated list, optional)
  • Apply the same escaping rules as the CLI.
  • Multiple variables are separated with semicolons (;)
  • Variables are applied only to the service process, not system-wide.

PowerShell Example

Import-Module "C:\Program Files\Servy\Servy.psm1" -Force

$installParams = @{
    Quiet        = $true
    Name         = "My NodeJS Service"
    Description  = "My NodeJS Server"
    Path         = "C:\Program Files\nodejs\node.exe"
    StartupDir   = "C:\Apps\App"
    Params       = "C:\Apps\App\index.js"
    StartupType  = "Automatic"

    Env          = "NODE_ENV=production; APP_CONFIG=C:\Apps\App\config.json"
}

Install-ServyService @installParams

Tips

  • Case Insensitivity: Environment variable names are case-insensitive. Defining node_env will correctly override an existing NODE_ENV.
  • Expansion Order: You can reference both existing system variables (like %ProgramData%) and other custom variables defined in the same list.
  • Safe Escaping: If your variable value must contain a literal %, ensure it is not wrapped in another % or it may be treated as a failed expansion.
  • Verification: To troubleshoot, if you aren't sure if your variables are applying correctly, run this to dump the environment to a file (PowerShell Admin):
    servy-cli install --name="EnvTest" --path="C:\Windows\System32\cmd.exe" --params="/c set > %TEMP%\servy_env.txt && pause" --env="MY_ROOT=C:\ServyApp; MY_LOGS=%MY_ROOT%\logs"
    servy-cli restart --name="EnvTest"
    Get-Content C:\Windows\Temp\servy_env.txt | Select-String "MY_LOGS"

Clone this wiki locally