Categorygithub.com/lib-x/winsvc
modulepackage
0.1.0
Repository: https://github.com/lib-x/winsvc.git
Documentation: pkg.go.dev

# README

Windows Service Library for Go

GoDoc Go Report Card

A Go library for creating and managing Windows services with ease.

Features

  • Install, uninstall, start, and stop Windows services
  • Run your Go application as a Windows service
  • Flexible configuration options for service installation
  • Support for both standard and advanced installation methods

Installation

go get github.com/lib-x/winsvc

Usage

Basic Example

Here's a simple example of how to use the library:

package main

import (
	"fmt"
	"log"

	"github.com/lib-x/winsvc"
)

func main() {
	if winsvc.InServiceMode() {
		err := winsvc.RunAsService("MyService", startServer, stopServer, false)
		if err != nil {
			log.Fatalf("Failed to run as service: %v", err)
		}
		return
	}

	// Run as a normal application
	startServer()
}

func startServer() {
	fmt.Println("Server starting...")
	// Your server logic here
}

func stopServer() {
	fmt.Println("Server stopping...")
	// Your cleanup logic here
}

Advanced Installation with Options

You can use the optional installation method for more control over service configuration:

package main

import (
	"flag"
	"fmt"
	"log"

	"github.com/lib-x/winsvc"
)

var (
	flagServiceName        = flag.String("name", "MyService", "Service name")
	flagServiceDisplayName = flag.String("display", "My Service Display Name", "Service display name")
	flagServiceDesc        = flag.String("desc", "My service description", "Service description")
	flagServiceInstall     = flag.Bool("install", false, "Install the service")
)

func main() {
	flag.Parse()

	if *flagServiceInstall {
		if err := installService(); err != nil {
			log.Fatalf("Failed to install service: %v", err)
		}
		fmt.Println("Service installed successfully")
		return
	}

	// Other service operations or normal application logic
}

func installService() error {
	exePath, err := winsvc.GetAppPath()
	if err != nil {
		return fmt.Errorf("failed to get executable path: %w", err)
	}

	options := []winsvc.ServiceOption{
		winsvc.DisplayName(*flagServiceDisplayName),
		winsvc.Description(*flagServiceDesc),
		winsvc.AutoStart(),
		winsvc.Dependencies("dependency1", "dependency2"),
	}

	return winsvc.InstallServiceWithOption(exePath, *flagServiceName, nil, options...)
}

This example demonstrates how to:

  • Use command-line flags to configure service properties
  • Get the current executable path
  • Use various service options like display name, description, start type, and dependencies
  • Install the service with custom options

To install the service with this configuration, you would run:

yourprogram.exe -install -name "MyCustomService" -display "My Custom Service" -desc "This is a custom Windows service"

API Reference

For detailed API documentation, please refer to the GoDoc.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

This project was originally forked from chai2010/winsvc. We appreciate their initial work on this library.

Reporting Issues

If you encounter any bugs or have feature requests, please file an issue on the GitHub issue tracker.

Contact

For any questions or support, please contact [email protected].

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
GetAppPath returns the absolute path of the current executable.
No description provided by the author
InServiceMode returns true if the current process is running as a Windows service.
No description provided by the author
InstallService installs a Windows service with the given parameters.
No description provided by the author
InstallServiceWithOption installs a Windows service with custom options.
No description provided by the author
IsAnInteractiveSession returns true if the current process is running in an interactive session.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
QueryService returns the current status of a Windows service.
No description provided by the author
RemoveService removes a Windows service with the given name.
No description provided by the author
RunAsService runs the provided start and stop functions as a Windows service.
No description provided by the author
StartService starts a Windows service with the given name.
No description provided by the author
StopService stops a Windows service with the given name.
No description provided by the author

# Type aliases

No description provided by the author