mirror of
https://github.com/kardianos/service.git
synced 2025-10-25 01:30:24 +08:00
Add support for retrieving service status (#143)
* update command execution to support returing error codes, and stdout add function to service interface to support returning service status clean up places where strings were being converted to strings * Spelling fix * Simply service Status method Add stopped detection to launchd Switch to case statements Return ErrNotInstalled in situations where we should
This commit is contained in:
committed by
Daniel Theophanes
parent
4cdeddd6f4
commit
45244176fc
@@ -8,10 +8,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"text/template"
|
||||
)
|
||||
@@ -57,13 +57,13 @@ func (s *systemd) configPath() (cp string, err error) {
|
||||
}
|
||||
|
||||
func (s *systemd) getSystemdVersion() int64 {
|
||||
out, err := exec.Command("/usr/bin/systemctl", "--version").Output()
|
||||
_, out, err := runWithOutput("systemctl", "--version")
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
re := regexp.MustCompile(`systemd ([0-9]+)`)
|
||||
matches := re.FindStringSubmatch(string(out))
|
||||
matches := re.FindStringSubmatch(out)
|
||||
if len(matches) != 2 {
|
||||
return -1
|
||||
}
|
||||
@@ -189,6 +189,24 @@ func (s *systemd) Run() (err error) {
|
||||
return s.i.Stop(s)
|
||||
}
|
||||
|
||||
func (s *systemd) Status() (Status, error) {
|
||||
exitCode, out, err := runWithOutput("systemctl", "is-active", s.Name)
|
||||
if exitCode == 0 && err != nil {
|
||||
return StatusUnknown, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(out, "active"):
|
||||
return StatusRunning, nil
|
||||
case strings.HasPrefix(out, "inactive"):
|
||||
return StatusStopped, nil
|
||||
case strings.HasPrefix(out, "failed"):
|
||||
return StatusUnknown, errors.New("service in failed state")
|
||||
default:
|
||||
return StatusUnknown, ErrNotInstalled
|
||||
}
|
||||
}
|
||||
|
||||
func (s *systemd) Start() error {
|
||||
return run("systemctl", "start", s.Name+".service")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user