// executes command in bash. Keeps track of process and kills it if it becomes terminated
// returns standard output, standard error, and result. result is <nil> if no error
executes command in bash. Keeps track of process and kills it if it becomes terminated
returns standard output, standard error, and result. result is  if no error
// executes command in bash . Keeps track of process and kills it if it becomes terminated // returns standard output standard error and result . result is <nil > if no error
func executeBashCmd(command string, abort <-chan bool) (string, string, error) {
    if abort == nil {
        abort = make(chan bool)
    }
    cmd := exec.Command("sh", "-c", command)
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        return "", "", err
    }
    stderr, err := cmd.StderrPipe()
    if err != nil {
        return "", "", err
    }
    if err := cmd.Start(); err != nil {
        return "", "", err
    }
    pid := cmd.Process.Pid
    stop := make(chan bool, 1)
    go func(){
        for {
            pingcmd := fmt.Sprintf("ps -p %v -o stat | grep -v STAT", pid)
            ping, _ := exec.Command("sh", "-c", pingcmd).Output()
            if string(ping[0:1]) == "T" {
                cmd.Process.Kill()
                return
            }
            select {
                case <-abort :
                    fmt.Println("kill me")
                    cmd.Process.Kill()
                    return
                case <-stop:
                    return
                default:
            }
            time.Sleep(time.Second * 10)
        }
    }()
    output := new(bytes.Buffer)
    output.ReadFrom(stdout)
    errout := new(bytes.Buffer)
    errout.ReadFrom(stderr)
    res := cmd.Wait()
    stop <- true
    return output.String(), errout.String(), res
}
func executeBashCmd ( command string , abort <- chan bool ) ( string , string , error ) { if abort == nil { abort = make ( chan bool ) } cmd := exec . Command ( " " , " " , command ) stdout , err := cmd . StdoutPipe ( ) if err != nil { return " " , " " , err } stderr , err := cmd . StderrPipe ( ) if err != nil { return " " , " " , err } if err := cmd . Start ( ) ; err != nil { return " " , " " , err } pid := cmd . Process . Pid stop := make ( chan bool , 1 ) go func ( ) { for { pingcmd := fmt . Sprintf ( " " , pid ) ping , _ := exec . Command ( " " , " " , pingcmd ) . Output ( ) if string ( ping [ 0 : 1 ] ) == " " { cmd . Process . Kill ( ) return } select { case <- abort : fmt . Println ( " " ) cmd . Process . Kill ( ) return case <- stop : return default : } time . Sleep ( time . Second * 10 ) } } ( ) output := new ( bytes . Buffer ) output . ReadFrom ( stdout ) errout := new ( bytes . Buffer ) errout . ReadFrom ( stderr ) res := cmd . Wait ( ) stop <- true return output . String ( ) , errout . String ( ) , res }
executes command in bash.
executes command in bash .
// create command //get stdout to read result // get error output // start command // get pid // check that executing process is still alive. kill if terminated // read any standard output into buffer // read any error output into buffer //Wait for command to finish // return result