From 2aef0c0ff92ea540e320fb88f8322f69d134d356 Mon Sep 17 00:00:00 2001 From: EnumDev Date: Sun, 16 Nov 2025 20:18:08 +0200 Subject: [PATCH] Improve StopService function --- src/esvm/service.go | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/esvm/service.go b/src/esvm/service.go index 0ab3da8..bfbe3b8 100644 --- a/src/esvm/service.go +++ b/src/esvm/service.go @@ -382,31 +382,33 @@ func (service *EnitService) StopService() error { service.GetProcess().Signal(syscall.SIGKILL) return fmt.Errorf("could not stop process gracefully") } - - // Check if the process has stopped gracefully, otherwise send sigkill on timeout - exited := make(chan bool) - go func() { - for { - if err := service.GetProcess().Signal(syscall.Signal(0)); err != nil { - break - } - } - exited <- true - }() - - select { - case <-exited: - case <-time.After(5 * time.Second): - service.GetProcess().Signal(syscall.SIGKILL) - return fmt.Errorf("could not stop process gracefully") - } } else { + go func() { service.stopChannel <- true }() + cmd := exec.Command("/bin/sh", "-c", service.StopCmd) if err := cmd.Run(); err != nil { return err } } + // Check if the process has stopped gracefully, otherwise send sigkill on timeout + exited := make(chan bool) + go func() { + for { + if err := syscall.Kill(pid, syscall.Signal(0)); err != nil { + break + } + } + exited <- true + }() + + select { + case <-exited: + case <-time.After(5 * time.Second): + service.GetProcess().Signal(syscall.SIGKILL) + return fmt.Errorf("could not stop process gracefully") + } + newServiceStatus = EnitServiceStopped logger.Printf("Service (%s) has stopped!\n", service.Name)