转到os / exec命令参数问题

时间:2018-06-21 21:20:19

标签: go operating-system

我在Go中编写了一些代码来调用vmstat来提取一些统计数据。 cmdArgs和我注释了几种变体,这些变体在起作用,而哪些不起作用:

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\n", 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 }

最重要的是,我想获取兆字节的数据,如果我在shell上运行以下命令,它将起作用:

# 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

我的问题是-安排参数并仍然获得所需输出的可接受方法是什么?另外,我对sar也有类似的问题(我正在尝试获取网络接口统计信息),但是我无法接受它来接受参数的任何变化:

  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\n", 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 }

这是我从外壳中调用sar的方法:

$ 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

感谢您的反馈/帮助。谢谢

1 个答案:

答案 0 :(得分:1)

使用以下内容:

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

命令参数与子进程的参数一对一映射。