Skip to content

Add procfs/allprocs #923

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/procfs/allprocs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app
27 changes: 27 additions & 0 deletions examples/procfs/allprocs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# これは何?

[procfs](https://github.com/prometheus/procfs) を使って、/procファイルシステム上の全プロセス情報を取得するサンプルです。

```sh
$ task
task: [default] rm -f ./app
task: [default] go build -o app .
task: [default] ./app
[01] 1: /.supervisor/supervisor
[02] 36: /.supervisor/supervisor
[03] 70: /usr/bin/dash
[04] 78: /ide/node
[05] 90: /usr/bin/bash
[06] 666: /ide/node
[07] 1286: /ide/node
[08] 1952: /ide/node
[09] 1977: /ide/node
[10] 2492: /ide/node
[11] 7471: /home/gitpod/go-packages/bin/gopls
[12] 7483: /home/gitpod/go-packages/bin/gopls
[13] 23327: /ide/node
[14] 42093: /home/gitpod/go-packages/bin/staticcheck
[15] 42113: /home/gitpod/go/bin/go
[16] 42122: /workspace/go/bin/task
[17] 42439: /workspace/try-golang/examples/procfs/allprocs/app
```
10 changes: 10 additions & 0 deletions examples/procfs/allprocs/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# https://taskfile.dev

version: '3'

tasks:
default:
cmds:
- rm -f ./app
- go build -o app .
- ./app
43 changes: 43 additions & 0 deletions examples/procfs/allprocs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"log"

"github.com/prometheus/procfs"
)

func main() {
log.SetFlags(0)

if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
var (
fs procfs.FS
err error
)
fs, err = procfs.NewFS(procfs.DefaultMountPoint)
if err != nil {
return err
}

var (
procs procfs.Procs // type Procs []Proc
)
procs, err = fs.AllProcs()
if err != nil {
return err
}

var (
drop = func(v string, _ error) string { return v }
)
for i, p := range procs {
log.Printf("[%02d] %7d: %s", i+1, p.PID, drop(p.Executable()))
}

return nil
}