cmdKit

package
v3.26.152 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LookPath func(file string) (string, error) = exec.LookPath

Functions

func NewCommand

func NewCommand(ctx context.Context, name string, args []string, options ...CmdOption) *exec.Cmd

NewCommand

PS: (1) Cmd.Start(): 不会阻塞; (2) Cmd.Run(): 会阻塞.

@param ctx (1) 如果命令在ctx超时之前没有完成,CommandContext将会杀死该进程;

(2) !!!: 存在部分杀不死的情况,比如yozo的logon.exe,ctx超时了进程还是卡死在那(通过go代码执行会卡死在那,且无法通过ctx结束;但直接在command里面执行就没问题).

@param args 可以为nil

func Run

func Run(ctx context.Context, name string, args ...string) ([]byte, *exec.Cmd, error)

Run 执行命令(会阻塞直到命令结束) - 只有 stdout 在 output 中,标准错误(stderr)会被丢弃或继承父进程的 stderr

!!!: (1) exec.Cmd结构体执行时,会处理路径中的空格(e.g. java可执行文件的绝对路径、-Djava.ext.dirs=的路径...) (2) 假如自行处理命令行中的路径,反而会导致: 命令执行失败

能从 err 中获取 stderr 的情况: 只有当命令以非零退出码结束时,Output() 才会返回 *ExitError,此时可以从中获取 stderr 无法从 err 中获取 stderr 的情况: 如果命令成功执行(退出码为 0),即使有 stderr 输出,Output() 也会返回 err = nil,stderr 内容会丢失

func RunCombinedly

func RunCombinedly(ctx context.Context, name string, args ...string) ([]byte, *exec.Cmd, error)

RunCombinedly 执行命令(会阻塞直到命令结束) - stdout 和 stderr 都在 output 中,两者的内容会合并到一起返回

func RunCombinedlyToString

func RunCombinedlyToString(ctx context.Context, name string, args ...string) (string, *exec.Cmd, error)

func RunToString

func RunToString(ctx context.Context, name string, args ...string) (string, *exec.Cmd, error)

Types

type CmdOption

type CmdOption func(opts *CmdOptions)

func WithPdeathsig

func WithPdeathsig(pdeathsig syscall.Signal) CmdOption

WithPdeathsig

适用环境: Linux

func WithSetpgid

func WithSetpgid(setpgid bool) CmdOption

WithSetpgid

适用环境: Linux、macOS

type CmdOptions

type CmdOptions struct {
	// Setpgid sets the process group ID of the child to Pgid,
	// or, if Pgid == 0, to the new child's process ID.
	Setpgid bool

	// Pdeathsig, if non-zero, is a signal that the kernel will send to
	// the child process when the creating thread dies. Note that the signal
	// is sent on thread termination, which may happen before process termination.
	// There are more details at https://go.dev/issue/27505.
	Pdeathsig syscall.Signal
}

CmdOptions 参考: exec_linux.go

func (CmdOptions) NewCommand

func (opts CmdOptions) NewCommand(ctx context.Context, name string, args ...string) *exec.Cmd

Jump to

Keyboard shortcuts

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