Os.Process.Wait () after os.FindProcess (pid) works with windows not on linux

I have a problem trying to restore a process in go. My application starts a bunch of processes, and when it crashes, the processes there are in the public domain, and when I restart my application, I want to restore my processes. In windows everything works as expected, I can wait() in the process of kill() , etc., but in linux it just goes through my wait() without any errors. 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 structure for processing processes. The important part is cmd, which from the "os/exec" package I also tried to call pr.wait() directly with the same problem

+6
source share
1 answer

You are not sending an error message from Wait . Try:

 ps, err := proc.Cmd.Wait() if err != nil { /* handle it */ } 

The documentation also says:

The wait waits for the process to complete and returns a ProcessState describing its status and error, if any. Awaiting release of any process related resources. On most operating systems, the process must be a child of the current process, or the error will be returned .

In your case, since you are β€œrecovering”, your process is not the parent of the processes found using os.FindProcess .

So why does it work on windows? I suspect that this is due to the fact that the windows comes down to WaitForSingleObject , which does not have this requirement.

+4
source

All Articles