winsvc

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 9 Imported by: 0

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 czyt.go@gmail.com.

Documentation

Overview

Package winsvc provides utilities for creating and managing Windows services.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAppPath

func GetAppPath() (string, error)

GetAppPath returns the absolute path of the current executable.

func InServiceMode

func InServiceMode() bool

InServiceMode returns true if the current process is running as a Windows service.

func InstallService

func InstallService(appPath, name, displayName, desc string, params ...string) error

InstallService installs a Windows service with the given parameters. It takes the application path, service name, display name, description, and optional parameters.

func InstallServiceWithOption

func InstallServiceWithOption(appPath, name string, serviceArgs []string, options ...ServiceOption) error

InstallServiceWithOption installs a Windows service with custom options. It takes the application path, service name, a ServiceArgsOption function, and variadic ServiceOption functions.

func IsAnInteractiveSession

func IsAnInteractiveSession() bool

IsAnInteractiveSession returns true if the current process is running in an interactive session.

func QueryService

func QueryService(name string) (string, error)

QueryService returns the current status of a Windows service.

func RemoveService

func RemoveService(name string) error

RemoveService removes a Windows service with the given name.

func RunAsService

func RunAsService(name string, start, stop func(), isDebug bool) error

RunAsService runs the provided start and stop functions as a Windows service. It takes the service name, start function, stop function, and a debug flag.

func StartService

func StartService(name string) error

StartService starts a Windows service with the given name.

func StopService

func StopService(name string) error

StopService stops a Windows service with the given name.

Types

type ServiceOption

type ServiceOption func(*mgr.Config)

func AutoDelayStart

func AutoDelayStart() ServiceOption

func AutoStart

func AutoStart() ServiceOption

func Dependencies

func Dependencies(serviceName ...string) ServiceOption

func Description

func Description(description string) ServiceOption

func DisabledStart

func DisabledStart() ServiceOption

func DisplayName

func DisplayName(displayName string) ServiceOption

func OnBootStart

func OnBootStart() ServiceOption

func OnDemandStart

func OnDemandStart() ServiceOption

func OnSystemStart

func OnSystemStart() ServiceOption

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL