I have an issue when trying to recover a process in go. My go app launch a bunch of processes and when it crashes the processes are out there in the open and when I rerun my app I want to recover my processes. On windows everything works as expected I can wait()
on the process kill()
it etc.. but in linux it just goes trough my wait()
without any error.
Here is the code
func (proc *process) Recover() {
pr, err := os.FindProcess(proc.Cmd.Process.Pid)
if err != nil {
return
}
log.Info("Recovering " + proc.Name + proc.Service.Version)
Processes.Lock()
Processes.Map[proc.Name] = proc
Processes.Unlock()
proc.Cmd.Process = pr
if proc.Service.Reload > 0 {
proc.End = make(chan bool)
go proc.KillRoutine()
}
proc.Cmd.Wait()
if proc.Status != "killed" {
proc.Status = "finished"
}
proc.Time = time.Now()
channelProcess <- proc
//confirmation that process was killed
if proc.End != nil {
proc.End <- true
}
}
process is my own struct to handle processes the important part is cmd which is from the package "os/exec"
I have also tried to directly call pr.wait()
with the same issue
You're not handing the error message from Wait
. Try:
ps, err := proc.Cmd.Wait()
if err != nil {
/* handle it */
}
Also the documentation says:
Wait waits for the Process to exit, and then returns a ProcessState describing its status and an error, if any. Wait releases any resources associated with the Process. On most operating systems, the Process must be a child of the current process or an error will be returned.
In your case since you're "recovering", your process is not the parent of the processes you found using os.FindProcess
.
So why does it work on windows? I suspect it is because on windows it boils down to WaitForSingleObject
which doesn't have that requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With