go exec 执行exe

示例

package main

import (
	"fmt"
	"os/exec"
	"time"
)

func main() {
	command := exec.Command("cmd", "/c", "start", "redis-server")
    // 第一个参数可以是环境变量path中的任何命令程序,或者指定具体的exe程序
    // 后面参数是任意可选的
    // 例如 cmd /c start 某个exe ,运行后开启新的cmnd窗口运行某个exe 
    // /c 是关闭原有窗口, 不加/c 开启新的失败,不知道什么原因
	err := command.Start() //start 是异步执行 run是同步
	if err != nil {
		panic(err)
	}
    //用于观察,可忽略
	tick := time.Tick(time.Second)
	for range tick {
		fmt.Println("....")
	}
}

猜你喜欢

转载自blog.csdn.net/takujo/article/details/108456527