service: move to windows svc package at x/sys/windows.
This commit is contained in:
+6
-22
@@ -5,17 +5,15 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.google.com/p/winsvc/eventlog"
|
"golang.org/x/sys/windows/svc"
|
||||||
"code.google.com/p/winsvc/mgr"
|
"golang.org/x/sys/windows/svc/eventlog"
|
||||||
"code.google.com/p/winsvc/svc"
|
"golang.org/x/sys/windows/svc/mgr"
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "Windows Service"
|
const version = "Windows Service"
|
||||||
@@ -169,20 +167,6 @@ func (ws *windowsService) Install() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
binPath := &bytes.Buffer{}
|
|
||||||
// Quote exe path in case it contains a string.
|
|
||||||
binPath.WriteRune('"')
|
|
||||||
binPath.WriteString(exepath)
|
|
||||||
binPath.WriteRune('"')
|
|
||||||
|
|
||||||
// Arguments are encoded with the binary path to service.
|
|
||||||
// Enclose arguments in quotes. Escape quotes with a backslash.
|
|
||||||
for _, arg := range ws.Arguments {
|
|
||||||
binPath.WriteRune(' ')
|
|
||||||
binPath.WriteString(`"`)
|
|
||||||
binPath.WriteString(strings.Replace(arg, `"`, `\"`, -1))
|
|
||||||
binPath.WriteString(`"`)
|
|
||||||
}
|
|
||||||
m, err := mgr.Connect()
|
m, err := mgr.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -193,14 +177,14 @@ func (ws *windowsService) Install() error {
|
|||||||
s.Close()
|
s.Close()
|
||||||
return fmt.Errorf("service %s already exists", ws.Name)
|
return fmt.Errorf("service %s already exists", ws.Name)
|
||||||
}
|
}
|
||||||
s, err = m.CreateService(ws.Name, binPath.String(), mgr.Config{
|
s, err = m.CreateService(ws.Name, exepath, mgr.Config{
|
||||||
DisplayName: ws.DisplayName,
|
DisplayName: ws.DisplayName,
|
||||||
Description: ws.Description,
|
Description: ws.Description,
|
||||||
StartType: mgr.StartAutomatic,
|
StartType: mgr.StartAutomatic,
|
||||||
ServiceStartName: ws.UserName,
|
ServiceStartName: ws.UserName,
|
||||||
Password: ws.Option.string("Password", ""),
|
Password: ws.Option.string("Password", ""),
|
||||||
Dependencies: ws.Dependencies,
|
Dependencies: ws.Dependencies,
|
||||||
})
|
}, ws.Arguments...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -278,7 +262,7 @@ func (ws *windowsService) Start() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
return s.Start([]string{})
|
return s.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *windowsService) Stop() error {
|
func (ws *windowsService) Stop() error {
|
||||||
|
|||||||
Reference in New Issue
Block a user