首页 > 系统服务 > 详细

1.9 进程pid,运行耗时 运行退出状态

时间:2018-03-17 22:47:57      阅读:235      评论:0      收藏:0      [点我收藏+]
package main

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

func main() {

    var cmd string
    if runtime.GOOS == "windows" {
        cmd = "timeout"
    } else {
        cmd = "sleep"
    }

    proc := exec.Command(cmd, "1")
    proc.Start()

    // Wait function will
    // wait till the process ends.
    proc.Wait()

    // After the process terminates
    // the *os.ProcessState contains
    // simple information
    // about the process run
    fmt.Printf("PID: %d\n", proc.ProcessState.Pid())
    fmt.Printf("Process took: %dms\n", proc.ProcessState.SystemTime()/time.Microsecond)
    fmt.Printf("Exited sucessfuly : %t\n", proc.ProcessState.Success())
}

/*
PID: 6337
Process took: 755ms
Exited sucessfuly : true
*/
package main

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

func main() {

    var cmd string
    if runtime.GOOS == "windows" {
        cmd = "timeout"
    } else {
        cmd = "sleep"
    }
    proc := exec.Command(cmd, "1")
    proc.Start()

    // No process state is returned
    // till the process finish.
    fmt.Printf("Process state for running process: %v\n", proc.ProcessState)

    // The PID could be obtain
    // event for the running process
    fmt.Printf("PID of running process: %d\n\n", proc.Process.Pid)
}

/*
Process state for running process: <nil>
PID of running process: 6410

*/

1.9 进程pid,运行耗时 运行退出状态

原文:https://www.cnblogs.com/zrdpy/p/8593254.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!