package main
import (
"os"
"time"
"fmt"
)
func main() {
//设置新进程的属性
attr := &os.ProcAttr{
//files指定新进程继承的活动文件对象
//前三个分别为,标准输入、标准输出、标准错误输出
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
//新进程的环境变量
Env: os.Environ(),
}
//win下通过记事本打开1.txt
//开始一个新进程
p, err := os.StartProcess("C:\\Windows\\System32\\notepad.exe", []string{"C:\\Windows\\System32\\notepad.exe", "D:\\1.txt"}, attr);
if err != nil {
fmt.Println(err);
}
fmt.Println(p);
fmt.Println("进程ID:", p.Pid);
//通过进程ID查找进程
p2, _ := os.FindProcess(p.Pid);
fmt.Println(p2);
//等待10秒,执行函数
time.AfterFunc(time.Second*10, func() {
//向p进程发送退出信号
p.Signal(os.Kill);
});
//等待进程p的退出,返回进程状态
ps, _ := p.Wait();
fmt.Println(ps.String());
}
转载于:https://my.oschina.net/u/921799/blog/3045749
1459

被折叠的 条评论
为什么被折叠?



