service: add Config.Executable field to allow service to control other binaries.

This commit is contained in:
Daniel Theophanes
2015-03-14 08:49:17 -07:00
parent 17f5541e81
commit 49d681e0cb
6 changed files with 344 additions and 339 deletions
+13
View File
@@ -64,6 +64,9 @@ package service // import "github.com/kardianos/service"
import ( import (
"errors" "errors"
"fmt" "fmt"
"path/filepath"
"github.com/kardianos/osext"
) )
// Config provides the setup for a Service. The Name field is required. // Config provides the setup for a Service. The Name field is required.
@@ -76,6 +79,9 @@ type Config struct {
UserName string // Run as username. UserName string // Run as username.
Arguments []string // Run with arguments. Arguments []string // Run with arguments.
// Optional field to specify the executable for service.
// If empty the current executable is used.
Executable string
WorkingDirectory string // Service working directory. WorkingDirectory string // Service working directory.
ChRoot string ChRoot string
UserService bool // Install as a current user service. UserService bool // Install as a current user service.
@@ -84,6 +90,13 @@ type Config struct {
Option KeyValue Option KeyValue
} }
func (c *Config) execPath() (string, error) {
if len(c.Executable) != 0 {
return filepath.Abs(c.Executable)
}
return osext.Executable()
}
var ( var (
system System system System
systemRegistry []System systemRegistry []System
+1 -3
View File
@@ -12,8 +12,6 @@ import (
"syscall" "syscall"
"text/template" "text/template"
"time" "time"
"github.com/kardianos/osext"
) )
const maxPathSize = 32 * 1024 const maxPathSize = 32 * 1024
@@ -98,7 +96,7 @@ func (s *darwinLaunchdService) Install() error {
} }
defer f.Close() defer f.Close()
path, err := osext.Executable() path, err := s.execPath()
if err != nil { if err != nil {
return err return err
} }
+1 -3
View File
@@ -12,8 +12,6 @@ import (
"syscall" "syscall"
"text/template" "text/template"
"time" "time"
"github.com/kardianos/osext"
) )
func isSystemd() bool { func isSystemd() bool {
@@ -75,7 +73,7 @@ func (s *systemd) Install() error {
} }
defer f.Close() defer f.Close()
path, err := osext.Executable() path, err := s.execPath()
if err != nil { if err != nil {
return err return err
} }
+1 -3
View File
@@ -12,8 +12,6 @@ import (
"syscall" "syscall"
"text/template" "text/template"
"time" "time"
"github.com/kardianos/osext"
) )
type sysv struct { type sysv struct {
@@ -67,7 +65,7 @@ func (s *sysv) Install() error {
} }
defer f.Close() defer f.Close()
path, err := osext.Executable() path, err := s.execPath()
if err != nil { if err != nil {
return err return err
} }
+5 -7
View File
@@ -8,12 +8,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"os/exec"
"os/signal" "os/signal"
"syscall"
"text/template" "text/template"
"time" "time"
"github.com/kardianos/osext"
) )
func isUpstart() bool { func isUpstart() bool {
@@ -77,7 +75,7 @@ func (s *upstart) Install() error {
} }
defer f.Close() defer f.Close()
path, err := osext.Executable() path, err := s.execPath()
if err != nil { if err != nil {
return err return err
} }
@@ -122,7 +120,7 @@ func (s *upstart) Run() (err error) {
sigChan := make(chan os.Signal, 3) sigChan := make(chan os.Signal, 3)
signal.Notify(sigChan, syscall.SIGTERM, os.Interrupt) signal.Notify(sigChan, os.Interrupt, os.Kill)
<-sigChan <-sigChan
@@ -130,11 +128,11 @@ func (s *upstart) Run() (err error) {
} }
func (s *upstart) Start() error { func (s *upstart) Start() error {
return run("initctl", "start", s.Name) return exec.Command("initctl", "start", s.Name).Run()
} }
func (s *upstart) Stop() error { func (s *upstart) Stop() error {
return run("initctl", "stop", s.Name) return exec.Command("initctl", "stop", s.Name).Run()
} }
func (s *upstart) Restart() error { func (s *upstart) Restart() error {
+2 -2
View File
@@ -16,7 +16,6 @@ import (
"code.google.com/p/winsvc/eventlog" "code.google.com/p/winsvc/eventlog"
"code.google.com/p/winsvc/mgr" "code.google.com/p/winsvc/mgr"
"code.google.com/p/winsvc/svc" "code.google.com/p/winsvc/svc"
"github.com/kardianos/osext"
) )
const version = "Windows Service" const version = "Windows Service"
@@ -165,10 +164,11 @@ loop:
} }
func (ws *windowsService) Install() error { func (ws *windowsService) Install() error {
exepath, err := osext.Executable() exepath, err := ws.execPath()
if err != nil { if err != nil {
return err return err
} }
binPath := &bytes.Buffer{} binPath := &bytes.Buffer{}
// Quote exe path in case it contains a string. // Quote exe path in case it contains a string.
binPath.WriteRune('"') binPath.WriteRune('"')