Documentation
¶
Overview ¶
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. * SPDX-License-Identifier: Apache-2.0
Index ¶
- func CancelExecCommand(cmd *exec.Cmd) (err error)
- func ConvertProcessError(err error) error
- func DefineCmdCancel(cmd *exec.Cmd) (*exec.Cmd, error)
- func InterruptProcess(ctx context.Context, pid int, signal InterruptType) (err error)
- func InterruptTypeStrings() []string
- func IsProcessRunning(ctx context.Context, pid int) (running bool, err error)
- func SetGroupAttrToCmd(c *exec.Cmd)
- func SleepCommand(duration time.Duration) *exec.Cmd
- func TerminateGracefully(ctx context.Context, pid int, gracePeriod time.Duration) (err error)
- func TerminateGracefullyWithChildren(ctx context.Context, pid int, gracePeriod time.Duration) (err error)
- func TerminateGracefullyWithChildrenWithSpecificInterrupts(ctx context.Context, pid int, gracePeriod time.Duration, ...) (err error)
- func TerminateGracefullyWithSpecificInterrupts(ctx context.Context, pid int, gracePeriod time.Duration, ...) (err error)
- func WaitForCompletion(ctx context.Context, pid int) (err error)
- func WaitForCompletionWithKill(ctx context.Context, pid int) (err error)
- type IProcess
- type InterruptType
- func (i InterruptType) IsAInterruptType() bool
- func (i InterruptType) MarshalJSON() ([]byte, error)
- func (i InterruptType) MarshalText() ([]byte, error)
- func (i InterruptType) MarshalYAML() (interface{}, error)
- func (i InterruptType) String() string
- func (i *InterruptType) UnmarshalJSON(data []byte) error
- func (i *InterruptType) UnmarshalText(text []byte) error
- func (i *InterruptType) UnmarshalYAML(unmarshal func(interface{}) error) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CancelExecCommand ¶
CancelExecCommand defines a more robust way to cancel subprocesses than what is done per default by [CommandContext](https://pkg.go.dev/os/exec#CommandContext).
This stops the subprocess tree but does not replace cmd.Wait(). Callers that started the command must still call cmd.Wait() to reap the process and release starter-owned handles. This is especially important on Windows, where a terminated process can remain observable until outstanding handles are closed.
func ConvertProcessError ¶
func DefineCmdCancel ¶
DefineCmdCancel sets and overwrites the cmd.Cancel function with CancelExecCommand so that it is more robust and thorough. This only changes cancellation behaviour; callers must still call cmd.Wait() after cmd.Start().
func InterruptProcess ¶
func InterruptProcess(ctx context.Context, pid int, signal InterruptType) (err error)
func InterruptTypeStrings ¶
func InterruptTypeStrings() []string
InterruptTypeStrings returns a slice of all String values of the enum
func IsProcessRunning ¶
IsProcessRunning states whether a process is running or not. An error is returned if the context is Done while looking for the process state.
func SetGroupAttrToCmd ¶
func SleepCommand ¶ added in v1.172.0
SleepCommand returns a command that sleeps for the requested duration using the current platform's native implementation.
func TerminateGracefully ¶
TerminateGracefully follows the pattern set by [kubernetes](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination) and terminates processes gracefully by first sending a SIGINT+SIGTERM and then a SIGKILL after the grace period has elapsed.
func TerminateGracefullyWithChildren ¶
func TerminateGracefullyWithChildren(ctx context.Context, pid int, gracePeriod time.Duration) (err error)
TerminateGracefullyWithChildren follows the pattern set by [kubernetes](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination) and terminates processes gracefully by first sending a SIGINT+SIGTERM and then a SIGKILL after the grace period has elapsed. It does not attempt to terminate the process group. If you wish to terminate the process group directly then send -pgid to TerminateGracefully but this does not guarantee that the group will be terminated gracefully. Instead, this function lists each child and attempts to kill them gracefully concurrently. It will then attempt to gracefully terminate itself. Due to the multi-stage process and the fact that the full grace period must pass for each stage specified above, the total maximum length of this function will be 2*gracePeriod not gracePeriod.
func TerminateGracefullyWithChildrenWithSpecificInterrupts ¶
func TerminateGracefullyWithChildrenWithSpecificInterrupts(ctx context.Context, pid int, gracePeriod time.Duration, interrupts ...InterruptType) (err error)
TerminateGracefullyWithChildrenWithSpecificInterrupts follows the pattern set by [kubernetes](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination) and terminates processes gracefully by first sending the specified interrupts and then a SIGKILL after the grace period has elapsed. It does not attempt to terminate the process group. If you wish to terminate the process group directly then send -pgid to TerminateGracefully but this does not guarantee that the group will be terminated gracefully. Instead, this function lists each child and attempts to kill them gracefully concurrently. It will then attempt to gracefully terminate itself. Due to the multi-stage process and the fact that the full grace period must pass for each stage specified above, the total maximum length of this function will be 2*gracePeriod not gracePeriod.
func TerminateGracefullyWithSpecificInterrupts ¶
func TerminateGracefullyWithSpecificInterrupts(ctx context.Context, pid int, gracePeriod time.Duration, interrupts ...InterruptType) (err error)
TerminateGracefullyWithSpecificInterrupts follows the pattern set by [kubernetes](https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-termination) and terminates processes gracefully by first sending the specified interrupts and then a SIGKILL after the grace period has elapsed.
func WaitForCompletion ¶
WaitForCompletion will wait for a given process to complete. This allows check to work if the underlying process was stopped without needing the os.Process that started it.
WaitForCompletion only observes process state by PID. It cannot reap a process or close handles held by the code that started it. Callers that own an *exec.Cmd must still call cmd.Wait() after cmd.Start(). This matters most on Windows, where a terminated process can remain observable until outstanding handles are closed, but other platforms can also continue to report an exited process until the starter has reaped it.
func WaitForCompletionWithKill ¶
WaitForCompletionWithKill will wait for a process to complete and if the context elapses then it will kill it and its children.
Like WaitForCompletion, this only observes and controls the process by PID. It does not reap a caller-owned *exec.Cmd or close handles held by the process starter, so callers that started the command must still call cmd.Wait().
Types ¶
type IProcess ¶
type IProcess interface {
// Pid is the process ID for this process.
Pid() int
// PPid is the parent process ID for this process.
PPid() int
// Executable name running this process. This is not a path to the
// executable.
Executable() string
// Name returns name of the process.
Name() string
Environ(ctx context.Context) ([]string, error)
// Cmdline returns the command line arguments of the process as a string with
// each argument separated by 0x20 ascii character.
Cmdline() string
// Cwd returns current working directory of the process.
Cwd() string
// Parent returns parent Process of the process.
Parent() IProcess
// Children returns the children of the process if any.
Children(ctx context.Context) ([]IProcess, error)
// IsAZombie returns whether the process is a zombie process. See https://en.wikipedia.org/wiki/Zombie_process
IsAZombie() bool
// IsRunning returns whether the process is still running or not.
IsRunning() bool
// Terminate sends SIGTERM to the process.
Terminate(context.Context) error
// Interrupt sends SIGINT to the process.
Interrupt(context.Context) error
// KillWithChildren sends SIGKILL to the process but also ensures any children of the process are also killed.
// see https://medium.com/@felixge/killing-a-child-process-and-all-of-its-children-in-go-54079af94773
// This method was introduced to avoid getting the following due to a poor cleanup:
// - Orphan processes (https://en.wikipedia.org/wiki/Orphan_process)
// - Zombies processes (https://en.wikipedia.org/wiki/Zombie_process)
KillWithChildren(context.Context) error
}
IProcess is the generic interface that is implemented on every platform and provides common operations for processes. Inspired from https://github.com/mitchellh/go-ps/blob/master/process.go
func FindProcess ¶
FindProcess looks up a single process by pid.
Process will be nil and error will be commonerrors.ErrNotFound if a matching process is not found.
func NewProcess ¶
NewProcess creates a new Process instance, it only stores the pid and checks that the process exists. Other method on Process can be used to get more information about the process. An error will be returned if the process does not exist.
func Ps ¶
Ps returns all processes in a similar fashion to `ps` command on Unix.
This of course will be a point-in-time snapshot of when this method was called. Some operating systems don't provide snapshot capability of the process table, in which case the process table returned might contain ephemeral entities that happened to be running when this was called.
type InterruptType ¶
type InterruptType int
const ( SigInt InterruptType = 2 SigKill InterruptType = 9 SigTerm InterruptType = 15 SubprocessTerminationGracePeriod = 10 * time.Millisecond )
func InterruptTypeString ¶
func InterruptTypeString(s string) (InterruptType, error)
InterruptTypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func InterruptTypeValues ¶
func InterruptTypeValues() []InterruptType
InterruptTypeValues returns all values of the enum
func (InterruptType) IsAInterruptType ¶
func (i InterruptType) IsAInterruptType() bool
IsAInterruptType returns "true" if the value is listed in the enum definition. "false" otherwise
func (InterruptType) MarshalJSON ¶
func (i InterruptType) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface for InterruptType
func (InterruptType) MarshalText ¶
func (i InterruptType) MarshalText() ([]byte, error)
MarshalText implements the encoding.TextMarshaler interface for InterruptType
func (InterruptType) MarshalYAML ¶
func (i InterruptType) MarshalYAML() (interface{}, error)
MarshalYAML implements a YAML Marshaler for InterruptType
func (InterruptType) String ¶
func (i InterruptType) String() string
func (*InterruptType) UnmarshalJSON ¶
func (i *InterruptType) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface for InterruptType
func (*InterruptType) UnmarshalText ¶
func (i *InterruptType) UnmarshalText(text []byte) error
UnmarshalText implements the encoding.TextUnmarshaler interface for InterruptType
func (*InterruptType) UnmarshalYAML ¶
func (i *InterruptType) UnmarshalYAML(unmarshal func(interface{}) error) error
UnmarshalYAML implements a YAML Unmarshaler for InterruptType
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors.
|
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. |
|
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors.
|
* Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. |