Skip to main content

Getting current process PID

· One min read

获取程序的pid很有用

拿到pid之后,你就可以使用操作系统自带的命令来查看更多关于这个pid的信息, eg

ps -p pid -v
package main

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

func main() {

pid := os.Getpid()
fmt.Printf("Process PID: %d \n", pid)

prc := exec.Command("ps", "-p", strconv.Itoa(pid), "-v")
out, err := prc.Output()
if err != nil {
panic(err)
}

fmt.Println(string(out))

}