github.com/jfrog/jfrog-cli-core/v2@v2.51.0/utils/osutils/utils_unix.go (about)

     1  //go:build linux || darwin || freebsd
     2  // +build linux darwin freebsd
     3  
     4  package osutils
     5  
     6  import (
     7  	"os"
     8  	"syscall"
     9  
    10  	"github.com/jfrog/jfrog-client-go/utils/log"
    11  )
    12  
    13  // This file will be compiled only on unix systems.
    14  // Checks if the process is running.
    15  // If error occurs, check if the error is part of the OS permission errors. This means the process is running.
    16  // Else means the process is not running.
    17  func IsProcessRunning(pid int) (bool, error) {
    18  	process, err := os.FindProcess(pid)
    19  	if err != nil {
    20  		return false, err
    21  	}
    22  	err = process.Signal(syscall.Signal(0))
    23  	// If err is not nil, then the other process might still be running.
    24  	if err != nil {
    25  		// If this is a permission error, then the other process is running
    26  		if os.IsPermission(err) {
    27  			log.Debug("Other process still alive:", err.Error())
    28  			return true, nil
    29  		}
    30  		// The other process died without unlocking. Let's unlock.
    31  		return false, nil
    32  	}
    33  	// This means there were no errors, same process.
    34  	return true, nil
    35  }