1 Star 0 Fork 1

mnt.ltd / command

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
command.go 983 Bytes
一键复制 编辑 原始数据 按行查看 历史
package command
import (
"os"
"sync"
"time"
)
type Option struct {
Timeout *time.Duration
Stdout *os.File
Stderr *os.File
Callback func(pid int)
}
// pidMap 用于存储所有的子进程pid,以便在主程序退出时,kill掉所有的相关子进程
var pidMap sync.Map
// 当主程序退出时,从pidMap中获取所有的pid,然后kill掉
func CloseChildProccess(pid ...int) {
if len(pid) > 0 {
for _, p := range pid {
closeProcess(p)
}
return
}
pidMap.Range(func(key, value interface{}) bool {
if pid, ok := value.(int); ok {
closeProcess(pid)
}
return true
})
}
// ExecCommand 执行cmd命令操作
func ExecCommand(name string, args []string, timeout ...time.Duration) (out string, err error) {
opt := Option{}
if len(timeout) > 0 {
opt.Timeout = &timeout[0]
}
return execCommand(name, args, opt)
}
func ExecCommandV2(name string, args []string, opt Option) (out string, err error) {
return execCommand(name, args, opt)
}
1
https://gitee.com/mnt-ltd/command.git
git@gitee.com:mnt-ltd/command.git
mnt-ltd
command
command
master

搜索帮助