cmdKit

package
v3.1.101 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func Execute

func Execute(ctx context.Context, name string, args ...string) ([]byte, error)

Execute 执行命令(会阻塞直到命令结束)

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

func ExecuteToString

func ExecuteToString(ctx context.Context, name string, args ...string) (string, error)

ExecuteToString 执行命令(会阻塞直到命令结束)

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

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