Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

windows

go

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

like image 583
Lomithrani Avatar asked Jun 06 '16 18:06

Lomithrani


1 Answers

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.

like image 160
cnicutar Avatar answered Sep 28 '22 06:09

cnicutar