golang調用shell命令標准輸出阻塞管道


 

 

package tools

import (
 "bufio"
 "errors"
 "fmt"
 "github.com/Sirupsen/logrus"
 "go.pkg.okcoin.com/devops/agent/internal/constant"
 "os/exec"
 "strings"
)

/**
return cmdlog,error
*/
func ExecShell(shellPath ...string) (string, error) {
 cmd := exec.Command("/bin/sh", shellPath...)
 stdout, err := cmd.StdoutPipe()
 if err != nil {
  logrus.Errorf("StdoutPipe:%s ", err.Error())
  return fmt.Sprintf("StdoutPipe:%s ", err.Error()), err
 }

 stderr, err := cmd.StderrPipe()
 if err != nil {
  logrus.Errorf("StderrPipe:%s ", err.Error())
  return fmt.Sprintf("StderrPipe:%s ", err.Error()), err
 }

 if err := cmd.Start(); err != nil {
  logrus.Errorf("Start:%s ", err.Error())
  return fmt.Sprintf("Start:%s ", err.Error()), err
 }

 //讀取標准輸出
 strStdout,_ := rsyncStd(bufio.NewReader(stdout))
 //讀取標准錯誤輸出
 strStderr,_ := rsyncStd(bufio.NewReader(stderr))

 //不包含tail -f(這個是配合業務日志輸出) GROUP_ID(以root用戶執行admin用戶的操作) NEW_PASSWD(創建用戶)
 if strStderr != "" &&
  !strings.Contains(strStderr, constant.SHELL_EXEC_LOG_TAILF) &&
  !strings.Contains(strStderr, constant.SHELL_EXEC_LOG_GROUP_ID) &&
  !strings.Contains(strStderr, constant.SHELL_EXEC_LOG_NEW_PWD) {
  logrus.Errorf("exec shell fail!err:%s ,execLog:%s", strStderr, strStdout)
  return strStderr, errors.New(fmt.Sprintf("exec fail!err:%s,execLog:%s", strStderr, strStdout))
 }
 if strStderr != "" {
  logrus.Errorf("exec shell fail!err:%s ,execLog:%s", strStderr, strStdout)
 }

 if err := cmd.Wait(); err != nil {
  logrus.Errorf("Wait:%s ,execLog:%s", err.Error(), strStdout)
  return fmt.Sprintf("Wait:%s,execLog:%s ", err.Error(), strStdout), err
 }

 return strStdout, nil
}

func rsyncStd(stdBuf *bufio.Reader)(string ,error){
 var lines []string
 for {
  b, _, err := stdBuf.ReadLine()
  if err != nil {
   if err.Error() != "EOF" {
    return "", err
   }
   break
  }
  lines = append(lines, string(b))
 }
 return strings.Join(lines,"\n"),nil
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM