Windows service wrapper for running a child console application under the Service Control Manager.
- Detects whether it was started by the Service Control Manager or from a console.
- Supports
installanduninstallcommands when run from the command line. - Starts the configured child console application when the service starts.
- Sends
CTRL+Cto the child process on service stop. - Waits 10 seconds before forcefully terminating the child process.
- Stops the wrapper service when the child process exits.
- Logs child startup failures to the Windows Application event log.
The Visual Studio project currently builds the executable as:
bin\sw.exe
sw install --name <service-name> --target <child-exe> [options]
sw uninstall --name <service-name>
-
--name <service-name>Service name to create in the Service Control Manager. -
--target <child-exe>Full path to the child console executable to launch when the service starts. -
--child-args <raw arguments>Raw command line passed to the child process after the executable path. -
--display-name <display name>Optional display name shown by Windows Services. Defaults to the service name. -
--account <user account>Service logon account. Defaults toLocalSystem. -
--password <password>Password for--accountwhen required by the selected Windows account. -
--startup manual|auto|delayed-autoService startup mode. Defaults tomanual. -
--restart-count <n>Number of restart attempts after a crash. Defaults to3. -
--restart-delay-ms <ms>Delay between restart attempts in milliseconds. Defaults to5000. -
--restart-reset-seconds <s>Failure count reset period in seconds. Defaults to86400.
Install a service with manual startup:
sw install --name MyWorker --target "C:\Apps\Worker\worker.exe"Install a delayed auto-start service with child arguments:
sw install --name MyWorker `
--display-name "My Worker Service" `
--target "C:\Apps\Worker\worker.exe" `
--child-args "--port 8080 --config C:\Apps\Worker\worker.json" `
--startup delayed-autoInstall using a custom account:
sw install --name MyWorker `
--target "C:\Apps\Worker\worker.exe" `
--account ".\svc-worker" `
--password "secret"Uninstall the service:
sw uninstall --name MyWorkerWhen the service is installed, the wrapper registers itself as the service binary and stores the child executable path and optional child arguments in its own service command line.
At service start:
- The wrapper starts the configured child process in a new process group.
- The service reports
SERVICE_RUNNINGafter the child starts successfully.
At service stop:
- The wrapper sends
CTRL+Cto the child console process. - It waits up to 10 seconds for the child to exit cleanly.
- If the child is still running, it terminates it.
If the child process exits on its own, the wrapper service also stops.
If the wrapper fails to start the child process, it writes an error entry to the Windows Application log using the event source:
ServiceWrapper
- This wrapper is intended for console child processes that can handle
CTRL+C. - The
--child-argsvalue is passed as raw text toCreateProcessW. - Running the internal
--servicemode directly from a console is not supported; it is intended to be launched only by the Service Control Manager.