Documentation
¶
Overview ¶
Package proc provides process management utilities including process information, signal handling, graceful shutdown, and command execution.
Index ¶
- Variables
- func Cancel(ids ...uint32)
- func Context() context.Context
- func Exec(ctx context.Context, opts ExecOptions) error
- func Name() string
- func Notify(sig os.Signal) bool
- func On(sig os.Signal, fn func()) uint32
- func Once(sig os.Signal, fn func()) uint32
- func Path(components ...string) string
- func Pathf(format string, args ...any) string
- func Pid() int
- func SetSysProcAttribute(cmd *exec.Cmd)
- func SetTimeToForceQuit(duration time.Duration)
- func Shutdown(sig syscall.Signal) error
- func Wait(sig os.Signal)
- func WorkDir() string
- type ExecOptions
Constants ¶
This section is empty.
Variables ¶
var Logger io.Writer
Logger is the output destination for debug messages. By default, it's set to os.Stdout in the init function. Set to io.Discard to disable debug logging.
Functions ¶
func Cancel ¶
func Cancel(ids ...uint32)
Cancel removes the signal listeners with the specified IDs. It's safe to pass IDs that don't exist or have already been removed. Zero IDs are ignored.
func Exec ¶
func Exec(ctx context.Context, opts ExecOptions) error
Exec executes a command with the given context and options. It supports timeout, graceful shutdown with configurable kill delay, and proper process group management to prevent zombie processes.
References: - https://github.com/gouravkrosx/golang-cmd-exit-demo?ref=hackernoon.com - https://keploy.io/blog/technology/managing-go-processes
func Notify ¶
Notify dispatches a signal to all registered listeners for that signal. It executes all matching listeners concurrently in separate goroutines, with panic recovery. Listeners registered with Once are automatically removed after execution.
Returns true if at least one listener was notified, false if no listeners were registered for the signal or if the signal is invalid.
func On ¶
On registers a signal handler that will be called every time the specified signal is received. Returns a unique ID that can be used with Cancel to remove the listener.
func Once ¶
Once registers a signal handler that will be called at most once when the specified signal is received. After execution, the listener is automatically removed. Returns a unique ID that can be used with Cancel to remove the listener before it executes.
func SetSysProcAttribute ¶
SetSysProcAttribute sets the system-specific process attributes for Unix-like systems. It configures the command to use Setpgid to create a new process group, which ensures that child processes can be properly reaped when the parent is killed, preventing defunct (zombie) processes.
This is particularly important when SubProcessA spawns SubProcessB and SubProcessA gets killed by context timeout - Setpgid ensures that SubProcessB can still be properly cleaned up.
func SetTimeToForceQuit ¶
SetTimeToForceQuit sets the duration to wait before forcefully killing the process during shutdown. If set to 0, the process will be killed immediately without attempting graceful shutdown.
func Shutdown ¶
Shutdown performs a graceful shutdown by notifying all registered signal listeners and optionally waiting for a configured delay before force killing.
If delayTimeBeforeForceQuit > 0, it will:
- Send SIGTERM to all registered listeners in a goroutine
- Wait for delayTimeBeforeForceQuit duration
- Force kill the process if still alive
If delayTimeBeforeForceQuit == 0, it will:
- Send SIGTERM to all registered listeners synchronously
- Immediately kill the process
func Wait ¶
Wait blocks until the specified signal is received. It registers a one-time signal handler and blocks the current goroutine until the signal arrives. This is useful for waiting for specific signals in a synchronous manner.
Example:
// Wait for SIGUSR1 signal
proc.Wait(syscall.SIGUSR1)
fmt.Println("Received SIGUSR1")
Types ¶
type ExecOptions ¶
type ExecOptions struct {
// WorkDir specifies the working directory for the command.
// If empty, defaults to the current process's working directory.
WorkDir string
// Timeout specifies the maximum duration for command execution.
// If > 0, a timeout context will be created.
Timeout time.Duration
// Env specifies additional environment variables to pass to the command.
// These are appended to the current process's environment.
Env []string
// Stdin specifies the standard input for the command.
Stdin io.Reader
// Stdout specifies the standard output for the command.
Stdout io.Writer
// Stderr specifies the standard error output for the command.
Stderr io.Writer
// Command specifies the command to execute.
Command string
// Args specifies the command arguments.
Args []string
// TTK (Time To Kill) specifies the delay between sending interrupt signal
// and kill signal during command cancellation.
TTK time.Duration
// OnStart is a callback function invoked after the command starts.
OnStart func(cmd *exec.Cmd)
}
ExecOptions configures command execution parameters.