mirror of
https://github.com/kardianos/service.git
synced 2025-10-06 01:07:01 +08:00
service: return better error messages from executed commands.
This commit is contained in:
@@ -65,9 +65,9 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
svcConfig := &service.Config{
|
svcConfig := &service.Config{
|
||||||
Name: "GoServiceTest",
|
Name: "GoServiceExampleLogging",
|
||||||
DisplayName: "Go Service Test",
|
DisplayName: "Go Service Example for Logging",
|
||||||
Description: "This is a test Go service.",
|
Description: "This is an example Go service that outputs log messages.",
|
||||||
}
|
}
|
||||||
|
|
||||||
prg := &program{}
|
prg := &program{}
|
||||||
|
@@ -30,9 +30,9 @@ func (p *program) Stop(s service.Service) error {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
svcConfig := &service.Config{
|
svcConfig := &service.Config{
|
||||||
Name: "GoServiceTest",
|
Name: "GoServiceExampleSimple",
|
||||||
DisplayName: "Go Service Test",
|
DisplayName: "Go Service Example",
|
||||||
Description: "This is a test Go service.",
|
Description: "This is an example Go service.",
|
||||||
}
|
}
|
||||||
|
|
||||||
prg := &program{}
|
prg := &program{}
|
||||||
|
@@ -7,7 +7,6 @@ package service
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"os/user"
|
"os/user"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -143,16 +142,14 @@ func (s *darwinLaunchdService) Start() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cmd := exec.Command("launchctl", "load", confPath)
|
return run("launchctl", "load", confPath)
|
||||||
return cmd.Run()
|
|
||||||
}
|
}
|
||||||
func (s *darwinLaunchdService) Stop() error {
|
func (s *darwinLaunchdService) Stop() error {
|
||||||
confPath, err := s.getServiceFilePath()
|
confPath, err := s.getServiceFilePath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cmd := exec.Command("launchctl", "unload", confPath)
|
return run("launchctl", "unload", confPath)
|
||||||
return cmd.Run()
|
|
||||||
}
|
}
|
||||||
func (s *darwinLaunchdService) Restart() error {
|
func (s *darwinLaunchdService) Restart() error {
|
||||||
err := s.Stop()
|
err := s.Stop()
|
||||||
|
@@ -8,7 +8,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"text/template"
|
"text/template"
|
||||||
@@ -94,15 +93,18 @@ func (s *systemd) Install() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = exec.Command("systemctl", "enable", s.Name+".service").Run()
|
err = run("systemctl", "enable", s.Name+".service")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return exec.Command("systemctl", "daemon-reload").Run()
|
return run("systemctl", "daemon-reload")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *systemd) Uninstall() error {
|
func (s *systemd) Uninstall() error {
|
||||||
exec.Command("systemctl", "disable", s.Name+".service").Run()
|
err := run("systemctl", "disable", s.Name+".service")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
cp, err := s.configPath()
|
cp, err := s.configPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -139,11 +141,11 @@ func (s *systemd) Run() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *systemd) Start() error {
|
func (s *systemd) Start() error {
|
||||||
return exec.Command("systemctl", "start", s.Name+".service").Run()
|
return run("systemctl", "start", s.Name+".service")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *systemd) Stop() error {
|
func (s *systemd) Stop() error {
|
||||||
return exec.Command("systemctl", "stop", s.Name+".service").Run()
|
return run("systemctl", "stop", s.Name+".service")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *systemd) Restart() error {
|
func (s *systemd) Restart() error {
|
||||||
|
@@ -8,7 +8,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"text/template"
|
"text/template"
|
||||||
@@ -140,11 +139,11 @@ func (s *sysv) Run() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *sysv) Start() error {
|
func (s *sysv) Start() error {
|
||||||
return exec.Command("service", s.Name, "start").Run()
|
return run("service", s.Name, "start")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sysv) Stop() error {
|
func (s *sysv) Stop() error {
|
||||||
return exec.Command("service", s.Name, "stop").Run()
|
return run("service", s.Name, "stop")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sysv) Restart() error {
|
func (s *sysv) Restart() error {
|
||||||
|
@@ -9,6 +9,7 @@ package service
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/syslog"
|
"log/syslog"
|
||||||
|
"os/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newSysLogger(name string, errs chan<- error) (Logger, error) {
|
func newSysLogger(name string, errs chan<- error) (Logger, error) {
|
||||||
@@ -49,3 +50,12 @@ func (s sysLogger) Warningf(format string, a ...interface{}) error {
|
|||||||
func (s sysLogger) Infof(format string, a ...interface{}) error {
|
func (s sysLogger) Infof(format string, a ...interface{}) error {
|
||||||
return s.send(s.Writer.Info(fmt.Sprintf(format, a...)))
|
return s.send(s.Writer.Info(fmt.Sprintf(format, a...)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func run(command string, arguments ...string) error {
|
||||||
|
cmd := exec.Command(command, arguments...)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("%q failed: %v, %s", command, err, out)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@@ -8,7 +8,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"text/template"
|
"text/template"
|
||||||
@@ -131,11 +130,11 @@ func (s *upstart) Run() (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *upstart) Start() error {
|
func (s *upstart) Start() error {
|
||||||
return exec.Command("initctl", "start", s.Name).Run()
|
return run("initctl", "start", s.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *upstart) Stop() error {
|
func (s *upstart) Stop() error {
|
||||||
return exec.Command("initctl", "stop", s.Name).Run()
|
return run("initctl", "stop", s.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *upstart) Restart() error {
|
func (s *upstart) Restart() error {
|
||||||
|
Reference in New Issue
Block a user