2016-09-24 10:06
浏览 106


I'm trying to use os.StartProcess to start a process with arguments on a FreeBSD machine. After trying several different ways of adding the correct arguments and always getting errors I have set up this simple proof of concept.

package main

import (

func main() {
    command := "/usr/local/sbin/pkg"
    args := []string{"install"}

    procAttr := new(os.ProcAttr)
    procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
    if process, err := os.StartProcess(command, args, procAttr); err != nil {
    } else {
        log.Printf("%d", process.Pid)

The idea here is that if the system starts just pkg it will complain about missing arguments, but if it starts pkg install it will suggest the help for the install command. See below:

pkg: not enough arguments
Usage: pkg [-v] [-d] [-l] [-N] [-j <jail name or id>|-c <chroot path>|-r <rootdir>] [-C <configuration file>] [-R <repo config dir>] [-o var=value] [-4|-6] <command> [<args>]

For more information on available commands and options see 'pkg help'.


Usage: pkg install [-AfInFMqRUy] [-r reponame] [-Cgix] <pkg-name> ...

For more information see 'pkg help install'.

When I use the go code above it will complaint about missing arguments, so why is my argument 'install' not added to the command, and how to correct my code?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • duanli0687 2016-09-25 18:30

    The first element of args is the process name. args[1] is the actual first argument. This is just like the os.Args (and argv in C) lists work. To avoid dealing with these low level details you should just use the os/exec package to launch external processes.

    解决 无用
    打赏 举报