Run systemd commands with --user flag (#310)

When controlling a user service with systemd supply the --user flag
This commit is contained in:
tomfeigin
2022-04-27 15:10:17 +03:00
committed by GitHub
parent ed46af2312
commit ff1da96551
2 changed files with 11 additions and 3 deletions
+1
View File
@@ -0,0 +1 @@
.vscode/
+10 -3
View File
@@ -90,7 +90,7 @@ func (s *systemd) unitName() string {
}
func (s *systemd) getSystemdVersion() int64 {
_, out, err := runWithOutput("systemctl", "--version")
_, out, err := s.runWithOutput("systemctl", "--version")
if err != nil {
return -1
}
@@ -234,7 +234,7 @@ func (s *systemd) Run() (err error) {
}
func (s *systemd) Status() (Status, error) {
exitCode, out, err := runWithOutput("systemctl", "is-active", s.unitName())
exitCode, out, err := s.runWithOutput("systemctl", "is-active", s.unitName())
if exitCode == 0 && err != nil {
return StatusUnknown, err
}
@@ -244,7 +244,7 @@ func (s *systemd) Status() (Status, error) {
return StatusRunning, nil
case strings.HasPrefix(out, "inactive"):
// inactive can also mean its not installed, check unit files
exitCode, out, err := runWithOutput("systemctl", "list-unit-files", "-t", "service", s.unitName())
exitCode, out, err := s.runWithOutput("systemctl", "list-unit-files", "-t", "service", s.unitName())
if exitCode == 0 && err != nil {
return StatusUnknown, err
}
@@ -275,6 +275,13 @@ func (s *systemd) Restart() error {
return s.runAction("restart")
}
func (s *systemd) runWithOutput(command string, arguments ...string) (int, string, error) {
if s.isUserService() {
arguments = append(arguments, "--user")
}
return runWithOutput(command, arguments...)
}
func (s *systemd) run(action string, args ...string) error {
if s.isUserService() {
return run("systemctl", append([]string{action, "--user"}, args...)...)