duanfang7270 2017-09-17 12:37
浏览 602

Golang exec .bat或.exe文件,但未获得输出

I want to exec .exe file with golang and send the output value in web socket.

It's work great in linux, when I run an bash file but not working in windows with .exe or .bat file (.bat file run .exe file with some parameters). The program was executed but i never see and recive the output of script. My problem is just how to get the output of script in windows. If you can help me it's would be apreciated.

Thanks :)

cmd := exec.Command("cmd", "/C", "file.exe or file.bat", "parameters")
stdout, err := cmd.StdoutPipe()
if err != nil {
    panic(err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
    panic(err)
}
err = cmd.Start()
if err != nil {
    panic(err)
}

scannerOut := bufio.NewScanner(stdout)
for scannerOut.Scan() {

    log.Printf("%s", scannerOut.Text())

    err = socket.Emit("test", map[string]interface{}{"user": user, "name": Name, "data": scannerOut.Text()})
    if err != nil {
        log.Println(err)
    }
}

thanks for your help i tried that but again for me the script was executed but i don't have the output :/

output, err := exec.Command("cmd", "/C", cmdPath).CombinedOutput()
//cmd := exec.Command("ping", "google.fr", "-t")
//cmd := exec.Command("cmd", "/C", "C:/Users/Cyril/Desktop/zec_miner/miner", "--server", "zec-eu1.nanopool.org", "--user", "t1cjM242nws6QjfGWApeASG9DNDpsrHWv8A.ccl3ouf/cyril.connan@gmail.com", "--pass", "z", "--port", "6666")
/*stdout, err := cmd.StdoutPipe()*/
if err != nil {
    panic(err)
}

log.Printf("%s", string(output))

err = socket.Emit("test", map[string]interface{}{"user": user, "rig_name": rigName, "data": output})
if err != nil {
    log.Println(err)
}
  • 写回答

1条回答 默认 最新

  • doujuanju3076 2017-09-17 18:47
    关注

    I have created a little hello world program hello.exe that just prints "Hello World!" to stdout. Then the following program calls hello.exe and prints "Hello World!" as well.

    Maybe there is an error in your piping code or the scanner stuff, try a simple example first and see where it fails.

    package main
    
    import (
        "fmt"
        "os/exec"
    )
    
    func main() {
        output, err := exec.Command("cmd", "/C", "hello.exe").CombinedOutput()
        if err != nil {
            panic(err)
        }
        fmt.Println(string(output))
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配