github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zshell/shell_notwin.go (about) 1 //go:build !windows 2 // +build !windows 3 4 package zshell 5 6 import ( 7 "context" 8 "errors" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "strings" 13 "syscall" 14 ) 15 16 func RunNewProcess(file string, args []string) (pid int, err error) { 17 execSpec := &syscall.ProcAttr{ 18 Env: os.Environ(), 19 Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()}, 20 } 21 if tmp, _ := ioutil.TempDir("", ""); tmp != "" { 22 tmp = filepath.Dir(tmp) 23 if strings.HasPrefix(file, tmp) { 24 return 0, errors.New("temporary program does not support startup") 25 } 26 } 27 return syscall.ForkExec(file, args, execSpec) 28 } 29 30 func RunBash(ctx context.Context, command string) (code int, outStr, errStr string, err error) { 31 return ExecCommand(ctx, []string{ 32 "bash", 33 "-c", 34 command, 35 }, nil, nil, nil) 36 }