https://github.com/willibrandon/windowsservice
Pure Go Windows service implementation with zero dependencies
https://github.com/willibrandon/windowsservice
go golang scm service service-manager syscall win32 windows windows-api windows-service
Last synced: 6 months ago
JSON representation
Pure Go Windows service implementation with zero dependencies
- Host: GitHub
- URL: https://github.com/willibrandon/windowsservice
- Owner: willibrandon
- License: mit
- Created: 2025-07-21T06:00:54.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-07-21T06:53:19.000Z (6 months ago)
- Last Synced: 2025-07-21T08:15:08.911Z (6 months ago)
- Topics: go, golang, scm, service, service-manager, syscall, win32, windows, windows-api, windows-service
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# windowsservice
[](https://github.com/willibrandon/windowsservice/actions/workflows/test.yml)
[](https://pkg.go.dev/github.com/willibrandon/windowsservice)
Pure Go Windows service implementation with zero dependencies.
## Installation
```bash
go get github.com/willibrandon/windowsservice
```
## Usage
```go
package main
import "github.com/willibrandon/windowsservice"
func main() {
start := func() {
// Your service logic here
for {
doWork()
}
}
stop := func() {
// Cleanup
}
windowsservice.Run(start, stop)
}
```
## Running
### Console Mode
```bash
go run .
```
### Service Mode
```powershell
# Build
go build -o myservice.exe
# Install
sc create MyService binPath= "C:\path\to\myservice.exe"
# Start
sc start MyService
# Stop
sc stop MyService
# Delete
sc delete MyService
```
## Testing
Requires Administrator privileges:
```bash
go test -v ./...
```
## Implementation Details
The service uses direct Windows syscalls to `advapi32.dll` and `kernel32.dll` for SCM integration. No CGO or external dependencies required.
### Service Detection
Automatically detects execution context:
- Console: Runs directly for debugging
- Service: Integrates with Windows Service Control Manager
### Status Reporting
Properly reports service states:
- `SERVICE_RUNNING`
- `SERVICE_STOP_PENDING`
- `SERVICE_STOPPED`
## Example
See [examples/heartbeat](examples/heartbeat) for a complete example that writes heartbeat timestamps to demonstrate service lifecycle.
## License
[MIT](LICENSE)