覚えたら書く

IT関係のデベロッパとして日々覚えたことを書き残したいです。twitter: @yyoshikaw

Go言語 - プロセスの情報出したり、プロセスの一覧出したり

安直なのかもしれないですが、go-psライブラリのお世話になると簡単です


以下で取得しましょう

go get github.com/mitchellh/go-ps


実行したプロセスの情報取得

実行したプログラムそのもののプロセス情報を取得する例です

package main

import (
    "fmt"
    "os"

    "github.com/mitchellh/go-ps"
)

func main() {
    currenProccessInfo()
}

func currenProccessInfo() {
    pid := os.Getpid()

    pidInfo, _ := ps.FindProcess(pid)

    fmt.Printf("%+v\n", pidInfo)

    fmt.Printf("> PID          : %d\n", pidInfo.Pid())
    fmt.Printf("> PPID         : %d\n", pidInfo.PPid())
    fmt.Printf("> Process name : %s\n", pidInfo.Executable())

    ppidInfo, _ := ps.FindProcess(pidInfo.PPid())
    fmt.Printf("> Parent process name : %s\n", ppidInfo.Executable())
}


実行結果例:

&{pid:37003 ppid:35930 binary:process1}
> PID          : 37003
> PPID         : 35930
> Process name : process1
> Parent process name : bash


実行中プロセスの一覧表示

実行中のプロセスの一覧を表示します。(面倒なので取得したプロセスの構造体の内容そのまま表示しています)

package main

import (
    "fmt"
    "os"

    "github.com/mitchellh/go-ps"
)

func main() {
    processList()
}

func processList() {
    processes, err := ps.Processes()

    if err != nil {
        os.Exit(1)
    }

    for i, p := range processes {
        fmt.Printf("%d : %+v\n", i, p)
    }
}


実行結果例:

0 ; &{pid:36453 ppid:35930 binary:process1}
1 ; &{pid:36412 ppid:28367 binary:Google Chrome He}
2 ; &{pid:36411 ppid:28367 binary:Google Chrome He}
3 ; &{pid:36410 ppid:28367 binary:Google Chrome He}
4 ; &{pid:36409 ppid:28367 binary:Google Chrome He}
5 ; &{pid:36407 ppid:28367 binary:Google Chrome He}
6 ; &{pid:36405 ppid:28367 binary:Google Chrome He}
7 ; &{pid:36265 ppid:1 binary:quicklookd}
8 ; &{pid:36126 ppid:28367 binary:Google Chrome He}
9 ; &{pid:36125 ppid:28367 binary:Google Chrome He}
10 ; &{pid:36123 ppid:28367 binary:Google Chrome He}
11 ; &{pid:36122 ppid:28367 binary:Google Chrome He}
12 ; &{pid:36109 ppid:28367 binary:Google Chrome He}
13 ; &{pid:36061 ppid:1 binary:LookupViewServic}
14 ; &{pid:36058 ppid:28367 binary:Google Chrome He}
15 ; &{pid:36057 ppid:28367 binary:Google Chrome He}
16 ; &{pid:36056 ppid:28367 binary:Google Chrome He}
17 ; &{pid:36055 ppid:28367 binary:Google Chrome He}
18 ; &{pid:36054 ppid:28367 binary:Google Chrome He}
19 ; &{pid:36053 ppid:28367 binary:Google Chrome He}
20 ; &{pid:36052 ppid:28367 binary:Google Chrome He}
21 ; &{pid:36051 ppid:28367 binary:Google Chrome He}
22 ; &{pid:36044 ppid:1 binary:gocode}
23 ; &{pid:36028 ppid:28367 binary:Google Chrome He}
24 ; &{pid:35930 ppid:35929 binary:bash}
25 ; &{pid:35929 ppid:35864 binary:Code Helper}
26 ; &{pid:35922 ppid:28367 binary:Google Chrome He}
(長いので中略)
330 ; &{pid:67 ppid:1 binary:iconservicesagen}
331 ; &{pid:66 ppid:1 binary:iconservicesd}
332 ; &{pid:61 ppid:1 binary:mds}
333 ; &{pid:60 ppid:1 binary:warmd}
334 ; &{pid:58 ppid:1 binary:airportd}
335 ; &{pid:54 ppid:1 binary:logd}
336 ; &{pid:49 ppid:1 binary:mobileassetd}
337 ; &{pid:48 ppid:1 binary:powerd}
338 ; &{pid:47 ppid:1 binary:configd}
339 ; &{pid:46 ppid:1 binary:appleeventsd}
340 ; &{pid:43 ppid:1 binary:mediaremoted}
341 ; &{pid:41 ppid:1 binary:fseventsd}
342 ; &{pid:40 ppid:1 binary:kextd}
343 ; &{pid:39 ppid:1 binary:uninstalld}
344 ; &{pid:37 ppid:1 binary:UserEventAgent}
345 ; &{pid:36 ppid:1 binary:syslogd}