diff --git a/daemon_linux_systemd.go b/daemon_linux_systemd.go index d6e6cca..9046951 100644 --- a/daemon_linux_systemd.go +++ b/daemon_linux_systemd.go @@ -7,6 +7,7 @@ package daemon import ( "os" "os/exec" + "path" "regexp" "strings" "text/template" @@ -72,7 +73,7 @@ func (linux *systemDRecord) Install(args ...string) (string, error) { } defer file.Close() - execPatch, err := executablePath(linux.name) + execPath, err := executablePath(linux.name) if err != nil { return installAction + failed, err } @@ -85,12 +86,13 @@ func (linux *systemDRecord) Install(args ...string) (string, error) { if err := templ.Execute( file, &struct { - Name, Description, Dependencies, Path, Args string + Name, BaseName, Description, Dependencies, Path, Args string }{ linux.name, + path.Base(linux.name), linux.description, strings.Join(linux.dependencies, " "), - execPatch, + execPath, strings.Join(args, " "), }, ); err != nil { @@ -101,7 +103,7 @@ func (linux *systemDRecord) Install(args ...string) (string, error) { return installAction + failed, err } - if err := exec.Command("systemctl", "enable", linux.name+".service").Run(); err != nil { + if err := exec.Command("systemctl", "enable", path.Base(linux.name)+".service").Run(); err != nil { return installAction + failed, err } @@ -217,8 +219,8 @@ Requires={{.Dependencies}} After={{.Dependencies}} [Service] -PIDFile=/var/run/{{.Name}}.pid -ExecStartPre=/bin/rm -f /var/run/{{.Name}}.pid +PIDFile=/var/run/{{.BaseName}}.pid +ExecStartPre=/bin/rm -f /var/run/{{.BaseName}}.pid ExecStart={{.Path}} {{.Args}} Restart=on-failure diff --git a/helper.go b/helper.go index e4bd4c1..55096e2 100644 --- a/helper.go +++ b/helper.go @@ -10,6 +10,8 @@ import ( "errors" "os" "os/exec" + "path" + "path/filepath" "strconv" "strings" ) @@ -47,12 +49,35 @@ func ExecPath() (string, error) { // Lookup path for executable file func executablePath(name string) (string, error) { - if path, err := exec.LookPath(name); err == nil { - if _, err := os.Stat(path); err == nil { - return path, nil + if p, err := exec.LookPath(name); err == nil { + if _, err := os.Stat(p); err == nil { + cwd, _ := os.Getwd() + if strings.HasPrefix(p, "./") { + p = path.Join(cwd, path.Base(p)) + } + return p, nil } } - return os.Executable() + + return findBinaryPath() +} + +func findBinaryPath() (string, error) { + ctlExePath, err := os.Executable() + if err != nil { + return "./", err + } + + dir, err := filepath.EvalSymlinks(ctlExePath) + if err != nil { + return "./", err + } + + cwd, _ := os.Getwd() + if strings.HasPrefix(dir, "./") { + dir = path.Join(cwd, path.Base(dir)) + } + return filepath.Abs(filepath.Dir(dir)) } // Check root rights to use system service