process

package
v0.0.0-...-0eacd2e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2024 License: MIT Imports: 7 Imported by: 27

README

Process

Find and terminate processes by name or path.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindPIDsWithMatchFn

func FindPIDsWithMatchFn(matchFn MatchFn, log Log) ([]int, error)

FindPIDsWithMatchFn returns pids for processes matching function

func FindProcesses

func FindProcesses(matcher Matcher, wait time.Duration, delay time.Duration, log Log) ([]ps.Process, error)

FindProcesses returns processes containing string matching process path

func KillAll

func KillAll(matcher Matcher, log Log) (pids []int)

KillAll kills all processes that match

func KillPID

func KillPID(pid int, log Log) error

KillPID kills process at pid (sends a SIGKILL on unix)

func TerminateAll

func TerminateAll(matcher Matcher, killDelay time.Duration, log Log) []int

TerminateAll stops all processes with executable names that contains the matching string. It returns the pids that were terminated. This method only logs errors, if you need error handling, you can should use a different implementation.

func TerminateAllWithProcessesFn

func TerminateAllWithProcessesFn(fn processesFn, matchFn MatchFn, killDelay time.Duration, log Log) (terminatedPids []int)

TerminateAllWithProcessesFn stops processes processesFn that satify the matchFn. It returns the pids that were terminated. This method only logs errors, if you need error handling, you can should use a different implementation.

func TerminatePID

func TerminatePID(pid int, killDelay time.Duration, log Log) error

TerminatePID is an overly simple way to terminate a PID. On darwin and linux, it calls SIGTERM, then waits a killDelay and then calls SIGKILL. We don't mind if we call SIGKILL on an already terminated process, since there could be a race anyway where the process exits right after we check if it's still running but before the SIGKILL. The killDelay is not used on windows.

func WaitForExit

func WaitForExit(matcher Matcher, wait time.Duration, delay time.Duration, log Log) ([]ps.Process, error)

WaitForExit returns processes (if any) that are still running after wait

Types

type Log

type Log interface {
	Debugf(s string, args ...interface{})
	Infof(s string, args ...interface{})
	Warningf(s string, args ...interface{})
	Errorf(s string, args ...interface{})
}

Log is the logging interface for the process package

type MatchFn

type MatchFn func(ps.Process) bool

MatchFn is a process matching function

type MatchType

type MatchType string

MatchType is how to match

const (
	// PathEqual matches path equals string
	PathEqual MatchType = "path-equal"
	// PathContains matches path contains string
	PathContains MatchType = "path-contains"
	// PathPrefix matches path has string prefix
	PathPrefix MatchType = "path-prefix"
	// ExecutableEqual matches executable name equals string
	ExecutableEqual MatchType = "executable-equal"
)

type Matcher

type Matcher struct {
	// contains filtered or unexported fields
}

Matcher can match a process

func NewMatcher

func NewMatcher(match string, matchType MatchType, log Log) Matcher

NewMatcher returns a new matcher

func (*Matcher) ExceptPID

func (m *Matcher) ExceptPID(p int)

ExceptPID will not match specified pid

func (Matcher) Fn

func (m Matcher) Fn() MatchFn

Fn is the matching function

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL