github.com/influxdata/telegraf@v1.30.3/internal/process/process_posix.go (about)

     1  //go:build !windows
     2  
     3  package process
     4  
     5  import (
     6  	"context"
     7  	"os/exec"
     8  	"syscall"
     9  	"time"
    10  )
    11  
    12  func (p *Process) gracefulStop(ctx context.Context, cmd *exec.Cmd, timeout time.Duration) {
    13  	select {
    14  	case <-time.After(timeout):
    15  		if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
    16  			p.Log.Errorf("Error after sending SIGTERM signal to process: %v", err)
    17  		}
    18  	case <-ctx.Done():
    19  	}
    20  	select {
    21  	case <-time.After(timeout):
    22  		if err := cmd.Process.Kill(); err != nil {
    23  			p.Log.Errorf("Error after killing process: %v", err)
    24  		}
    25  	case <-ctx.Done():
    26  	}
    27  }