github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/util/process/process_unix.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package process
     5  
     6  import (
     7  	"os"
     8  	"syscall"
     9  
    10  	proc "github.com/shirou/gopsutil/process"
    11  )
    12  
    13  func Exists(pid int) bool {
    14  	p, err := proc.NewProcess(int32(pid))
    15  	if err != nil {
    16  		return false
    17  	}
    18  
    19  	s, err := p.Status()
    20  	if err != nil {
    21  		return false
    22  	}
    23  
    24  	return s != "Z"
    25  }
    26  
    27  // SendSignal sends signal s to the process p.
    28  //
    29  // The call ignores SIGCHLD (which is sent to the parent of a child process
    30  // when it exits, is interrupted, or resumes after being interrupted.)
    31  func SendSignal(p *os.Process, s os.Signal) error {
    32  	if s != syscall.SIGCHLD {
    33  		return p.Signal(s)
    34  	}
    35  	return nil
    36  }