drgmszo076956 2016-09-24 10:06
浏览 108
已采纳

如何在FreeBSD上向os.StartProcess添加参数?

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 (
    "log"
    "os"
)

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 {
        log.Println(err.Error())
    } 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'.

Or

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助