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 (
"errors"
"fmt"
"path/filepath"
"github.com/kardianos/osext"
)
// Config provides the setup for a Service. The Name field is required.
@@ -76,6 +79,9 @@ type Config struct {
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
WorkingDirectory string // Service working directory.
ChRoot string
UserService bool // Install as a current user service.
@@ -84,6 +90,13 @@ type Config struct {
Option KeyValue
}
func (c *Config) execPath() (string, error) {
if len(c.Executable) != 0 {
return filepath.Abs(c.Executable)
}
return osext.Executable()
}
var (
system System
systemRegistry []System
+1 -3
View File
@@ -12,8 +12,6 @@ import (
"syscall"
"text/template"
"time"
"github.com/kardianos/osext"
)
const maxPathSize = 32 * 1024
@@ -98,7 +96,7 @@ func (s *darwinLaunchdService) Install() error {
}
defer f.Close()
path, err := osext.Executable()
path, err := s.execPath()
if err != nil {
return err
}
+1 -3
View File
@@ -12,8 +12,6 @@ import (
"syscall"
"text/template"
"time"
"github.com/kardianos/osext"
)
func isSystemd() bool {
@@ -75,7 +73,7 @@ func (s *systemd) Install() error {
}
defer f.Close()
path, err := osext.Executable()
path, err := s.execPath()
if err != nil {
return err
}
+1 -3
View File
@@ -12,8 +12,6 @@ import (
"syscall"
"text/template"
"time"
"github.com/kardianos/osext"
)
type sysv struct {
@@ -67,7 +65,7 @@ func (s *sysv) Install() error {
}
defer f.Close()
path, err := osext.Executable()
path, err := s.execPath()
if err != nil {
return err
}
+5 -7
View File
@@ -8,12 +8,10 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
"text/template"
"time"
"github.com/kardianos/osext"
)
func isUpstart() bool {
@@ -77,7 +75,7 @@ func (s *upstart) Install() error {
}
defer f.Close()
path, err := osext.Executable()
path, err := s.execPath()
if err != nil {
return err
}
@@ -122,7 +120,7 @@ func (s *upstart) Run() (err error) {
sigChan := make(chan os.Signal, 3)
signal.Notify(sigChan, syscall.SIGTERM, os.Interrupt)
signal.Notify(sigChan, os.Interrupt, os.Kill)
<-sigChan
@@ -130,11 +128,11 @@ func (s *upstart) Run() (err 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 {
return run("initctl", "stop", s.Name)
return exec.Command("initctl", "stop", s.Name).Run()
}
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/mgr"
"code.google.com/p/winsvc/svc"
"github.com/kardianos/osext"
)
const version = "Windows Service"
@@ -165,10 +164,11 @@ loop:
}
func (ws *windowsService) Install() error {
exepath, err := osext.Executable()
exepath, err := ws.execPath()
if err != nil {
return err
}
binPath := &bytes.Buffer{}
// Quote exe path in case it contains a string.
binPath.WriteRune('"')