跨平台代码设计

// Config provides the setup for a Service. The Name field is required.
type Config struct {
Name string // Required name of the service. No spaces suggested.
DisplayName string // Display name, spaces allowed.
Description string // Long description of service.
UserName string // Run as username.
Arguments []string // Run with arguments.

// Optional field to specify the executable for service.
// If empty the current executable is used.
Executable string

// Array of service dependencies.
// Not yet fully implemented on Linux or OS X:
// 1. Support linux-systemd dependencies, just put each full line as the
// element of the string array, such as
// "After=network.target syslog.target"
// "Requires=syslog.target"
// Note, such lines will be directly appended into the [Unit] of
// the generated service config file, will not check their correctness.
Dependencies []string

// The following fields are not supported on Windows.
WorkingDirectory string // Initial working directory.
ChRoot string

// System specific options.
// * OS X
// - LaunchdConfig string () - Use custom launchd config
// - KeepAlive bool (true)
// - RunAtLoad bool (false)
// - UserService bool (false) - Install as a current user service.
// - SessionCreate bool (false) - Create a full user session.
// * POSIX
// - SystemdScript string () - Use custom systemd script
// - UpstartScript string () - Use custom upstart script
// - SysvScript string () - Use custom sysv script
// - RunWait func() (wait for SIGNAL) - Do not install signal but wait for this function to return.
// - ReloadSignal string () [USR1, ...] - Signal to send on reaload.
// - PIDFile string () [/run/prog.pid] - Location of the PID file.
// - LogOutput bool (false) - Redirect StdErr & StandardOutPath to files.
// - Restart string (always) - How shall service be restarted.
// - SuccessExitStatus string () - The list of exit status that shall be considered as successful,
// in addition to the default ones.
// * Linux (systemd)
// - LimitNOFILE int - Maximum open files (ulimit -n) (https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7)
// * Windows
// - DelayedAutoStart bool (false) - after booting start this service after some delay

Option KeyValue
}

github.com/kardianos/service@v1.2.0/service.go

// KeyValue provides a list of platform specific options. See platform docs for
// more details.
type KeyValue map[string]interface{}

windows

sc create

sc create --help
描述:
在注册表和服务数据库中创建服务项。
用法:
sc <server> create [service name] [binPath= ] <option1> <option2>...

选项:
注意: 选项名称包括等号。
等号和值之间需要一个空格。
type= <own|share|interact|kernel|filesys|rec|userown|usershare>
(默认 = own)
start= <boot|system|auto|demand|disabled|delayed-auto>
(默认 = demand)
error= <normal|severe|critical|ignore>
(默认 = normal)
binPath= <.exe 文件的 BinaryPathName>
group= <LoadOrderGroup>
tag= <yes|no>
depend= <依存关系(以 / (斜杠)分隔)>
obj= <AccountName|ObjectName>
(默认= LocalSystem)
DisplayName= <显示名称>
password= <密码>



op := service.KeyValue{"start": "auto"}
svcConfig := &service.Config{
Name: "AAAGoServiceExampleSimple",
DisplayName: "AAAGo Service Example",
Description: "This is an example Go service.",
Option: op,
}
原文地址:https://www.cnblogs.com/rsapaper/p/15512316.html