Skip to main content
โšก Calmops

Run a Program as a Windows Service at Startup with WinSW

Introduction

Windows Services start automatically at boot, before any user logs in, and run in the background. If you have a program (a Go binary, Python script, Node.js app, etc.) that you want to run as a service, WinSW is the easiest way to wrap it.

WinSW is a lightweight executable that wraps any program as a Windows service โ€” no coding required, just a simple XML config file.

Prerequisites

  • Windows 10/11 or Windows Server
  • Administrator access
  • Your program’s executable (e.g., dosomething.exe)

Step 1: Download WinSW

Download the latest WinSW.exe from the WinSW releases page.

Choose the right version:

  • WinSW-x64.exe โ€” for 64-bit Windows (most common)
  • WinSW-x86.exe โ€” for 32-bit Windows
  • WinSW-arm64.exe โ€” for ARM64

Rename it to WinSW.exe and place it in your working directory alongside your program.

Example directory structure:

D:\dosomething\
โ”œโ”€โ”€ dosomething.exe      โ† your program
โ”œโ”€โ”€ dosomething.ini      โ† your program's config
โ”œโ”€โ”€ WinSW.exe            โ† WinSW executable
โ””โ”€โ”€ winsw.xml            โ† WinSW config (you'll create this)

Step 2: Create the WinSW Config File

Create winsw.xml in the same directory:

<service>
    <!-- Unique service ID (no spaces) -->
    <id>dosomething</id>

    <!-- Display name in Services panel -->
    <name>dosomething</name>

    <!-- Description shown in Services panel -->
    <description>dosomething client service</description>

    <!-- Path to your executable (relative to winsw.xml location) -->
    <executable>dosomething</executable>

    <!-- Command-line arguments for your program -->
    <arguments>-c dosomething.ini</arguments>

    <!-- Log mode: reset (new log each start), roll, append -->
    <logmode>reset</logmode>
</service>

Extended Configuration Options

<service>
    <id>myapp</id>
    <name>My Application</name>
    <description>My application service</description>
    <executable>myapp.exe</executable>
    <arguments>--config config.yaml --port 8080</arguments>

    <!-- Working directory -->
    <workingdirectory>D:\myapp</workingdirectory>

    <!-- Environment variables -->
    <env name="APP_ENV" value="production"/>
    <env name="LOG_LEVEL" value="info"/>

    <!-- Restart on failure -->
    <onfailure action="restart" delay="10 sec"/>
    <onfailure action="restart" delay="20 sec"/>
    <onfailure action="none"/>

    <!-- Log configuration -->
    <log mode="roll-by-size">
        <sizeThreshold>10240</sizeThreshold>
        <keepFiles>8</keepFiles>
    </log>

    <!-- Start mode: Automatic, Manual, Boot, System -->
    <startmode>Automatic</startmode>

    <!-- Run as a specific user (optional) -->
    <!-- <serviceaccount>
        <username>.\LocalUser</username>
        <password>password</password>
    </serviceaccount> -->
</service>

Step 3: Register the Service

Open Command Prompt as Administrator and navigate to your working directory:

d:
cd dosomething

Install the service:

WinSW.exe install winsw.xml

You should see: 2023-08-07 10:00:00,000 INFO - Installing the service with id 'dosomething'

Step 4: Start the Service

The service is installed but not yet running. Start it:

WinSW.exe start winsw.xml

Or start it from the Windows Services panel:

  1. Press Win + R, type services.msc, press Enter
  2. Find your service by name
  3. Right-click โ†’ Start

Managing the Service

# Check service status
WinSW.exe status winsw.xml

# Stop the service
WinSW.exe stop winsw.xml

# Restart the service
WinSW.exe restart winsw.xml

# Uninstall the service
WinSW.exe uninstall winsw.xml

Or use the built-in sc command:

# Start
sc start dosomething

# Stop
sc stop dosomething

# Query status
sc query dosomething

# Delete service
sc delete dosomething

Or PowerShell:

# Start
Start-Service dosomething

# Stop
Stop-Service dosomething

# Get status
Get-Service dosomething

# Set to automatic start
Set-Service dosomething -StartupType Automatic

Step 5: Verify at Startup

Restart Windows and verify your service started automatically:

sc query dosomething

Look for STATE: 4 RUNNING.

Viewing Logs

WinSW creates log files in the same directory as winsw.xml:

D:\dosomething\
โ”œโ”€โ”€ dosomething.out.log   โ† stdout from your program
โ”œโ”€โ”€ dosomething.err.log   โ† stderr from your program
โ””โ”€โ”€ dosomething.wrapper.log โ† WinSW's own log

Troubleshooting

Service fails to start

Check dosomething.wrapper.log for errors. Common causes:

  • Wrong path to executable
  • Missing dependencies (DLLs, runtimes)
  • Insufficient permissions
# Run the executable manually first to check for errors
D:\dosomething\dosomething.exe -c dosomething.ini

Access denied errors

Run Command Prompt as Administrator. For services that need network access or specific file permissions, configure the service account in winsw.xml.

Service starts then immediately stops

Your program may be exiting. Check dosomething.err.log. Make sure your program runs as a foreground process (doesn’t daemonize itself).

Alternative: Native Windows Service Registration

For .NET applications or when you don’t want WinSW, use sc create directly:

sc create "MyService" ^
    binPath= "D:\myapp\myapp.exe --config config.yaml" ^
    DisplayName= "My Application" ^
    start= auto

sc description "MyService" "My application running as a Windows service"
sc start "MyService"

Alternative: NSSM (Non-Sucking Service Manager)

NSSM is another popular option with a GUI:

# Install
nssm install dosomething "D:\dosomething\dosomething.exe"

# Configure via GUI
nssm edit dosomething

# Start
nssm start dosomething

Resources

Comments