mirror of
https://github.com/EnumeratedDev/enit.git
synced 2026-09-16 02:26:11 +00:00
Improve StopService function
This commit is contained in:
+20
-18
@@ -382,31 +382,33 @@ func (service *EnitService) StopService() error {
|
|||||||
service.GetProcess().Signal(syscall.SIGKILL)
|
service.GetProcess().Signal(syscall.SIGKILL)
|
||||||
return fmt.Errorf("could not stop process gracefully")
|
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 {
|
} else {
|
||||||
|
go func() { service.stopChannel <- true }()
|
||||||
|
|
||||||
cmd := exec.Command("/bin/sh", "-c", service.StopCmd)
|
cmd := exec.Command("/bin/sh", "-c", service.StopCmd)
|
||||||
if err := cmd.Run(); err != nil {
|
if err := cmd.Run(); err != nil {
|
||||||
return err
|
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
|
newServiceStatus = EnitServiceStopped
|
||||||
logger.Printf("Service (%s) has stopped!\n", service.Name)
|
logger.Printf("Service (%s) has stopped!\n", service.Name)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user