duandi8838 2018-06-21 21:20
浏览 44

转到os / exec命令参数问题[重复]

I wrote some code in Go to call vmstat to pull some statistics data. There are several variations to the cmdArgs and I annotated which are working and which are not:

1 package main
  2 
  3 import (
  4   "bufio"
  5   "fmt"
  6   "os"
  7   "os/exec"
  8 )
  9 
 10 func main() {
 11   // cmdName := "sar"
 12   // cmdArgs := []string{"-n DEV 1 1"}
 13   cmdName := "vmstat"
 14 
 15   // cmdArgs := []string{"-tSm 1 1"}  //<--- This works but does
 16                                      //     not produce desired output
 17 
 18   cmdArgs := []string{"-t -Sm 1 1"}  //<--- This works but does
 19                                      //     not produce desired output
 20 
 21   // cmdArgs := []string{"-Sm 1 1", "-t"}  //<--- This does NOT work
 22   // cmdArgs := []string{"-t","-Sm 1 1"}   //<--- This does NOT work
 23 
 24   cmd := exec.Command(cmdName, cmdArgs...)
 25   cmdReader, err := cmd.StdoutPipe()
 26   if err != nil {
 27     fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
 28     os.Exit(1)
 29   }
 30 
 31   scanner := bufio.NewScanner(cmdReader)
 32   go func() {
 33     for scanner.Scan() {
 34       fmt.Printf("%s
", scanner.Text())
 35     }
 36   }()
 37 
 38   err = cmd.Start()
 39   if err != nil {
 40     fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
 41     os.Exit(1)
 42   }
 43 
 44   err = cmd.Wait()
 45   if err != nil {
 46     fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err)
 47     os.Exit(1)
 48   }
 49 }

Bottom line is I want to get the data in Megabytes and if I run the following on my shell, it works:

# vmstat -t -Sm 1 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  1    201   4544    190   7970    0    0    85    32    1    1  1  0 97  2  0    2018-06-21 16:52:13 EDT

# vmstat -Sm -t 1 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  1    201   4496    191   8012    0    0    85    32    1    1  1  0 97  2  0    2018-06-21 16:52:33 EDT

My question is - what is the acceptable way to arrange the arguments and still get the desired output? Also, I have a similar issue with sar(I am trying to pull network interface stats), but I cannot get it to accept any variation of the arguments:

  1 package main
  2 
  3 import (
  4   "bufio"
  5   "fmt"
  6   "os"
  7   "os/exec"
  8 )
  9 
 10 func main() {
 11   cmdName := "sar"
 12   cmdArgs := []string{"-n DEV 1 1"} //<--- This does NOT work
 13 
 14   // cmdArgs := []string{"-n DEV","1 1"} //<--- This does NOT work
 15 
 16   cmd := exec.Command(cmdName, cmdArgs...)
 17   cmdReader, err := cmd.StdoutPipe()
 18   if err != nil {
 19     fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
 20     os.Exit(1)
 21   }
 22 
 23   scanner := bufio.NewScanner(cmdReader)
 24   go func() {
 25     for scanner.Scan() {
 26       fmt.Printf("%s
", scanner.Text())
 27     }
 28   }()
 29 
 30   err = cmd.Start()
 31   if err != nil {
 32     fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
 33     os.Exit(1)
 34   }
 35 
 36   err = cmd.Wait()
 37   if err != nil {
 38     fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err)
 39     os.Exit(1)
 40   }
 41 }

Here is how I call sar from the shell:

$ sar -n DEV 1 1
Linux 2.6.32-696.18.7.el6.x86_64 (invenioq.gmu.edu)     06/21/2018  _x86_64_    (2 CPU)

05:08:56 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
05:08:57 PM        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
05:08:57 PM      eth0      3.96      0.00      0.24      0.00      0.00      0.00      0.00
05:08:57 PM      eth1      2.97      0.00      0.17      0.00      0.00      0.00      0.00

Average:        IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
Average:           lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:         eth0      3.96      0.00      0.24      0.00      0.00      0.00      0.00
Average:         eth1      2.97      0.00      0.17      0.00      0.00      0.00      0.00

I appreciate any feedback/help. Thank you

</div>
  • 写回答

1条回答 默认 最新

  • dongyutan1703 2018-06-21 21:30
    关注

    Use the following:

    cmdArgs := []string{"-t", "-Sm", "1", "1"}
    

    Command args map to one to one with arguments to the child process.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大