Skip to content

Commit d6b3a7f

Browse files
authored
Merge pull request #922 from devlights/add-procfs-self
2 parents 7039d09 + ee1decf commit d6b3a7f

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

examples/procfs/self/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
app

examples/procfs/self/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# これは何?
2+
3+
[procfs](https://github.com/prometheus/procfs) を使って、/procファイルシステム上の自プロセス情報を取得するサンプルです。
4+
5+
```sh
6+
$ task
7+
task: [default] rm -f ./app
8+
task: [default] go build -o app .
9+
task: [default] ./app
10+
[Self] pid=21966, cmdline=[./app]
11+
task: [default] ./app hello world 1 2 3 4 5
12+
[Self] pid=21972, cmdline=[./app hello world 1 2 3 4 5]
13+
```

examples/procfs/self/Taskfile.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# https://taskfile.dev
2+
3+
version: '3'
4+
5+
tasks:
6+
default:
7+
cmds:
8+
- rm -f ./app
9+
- go build -o app .
10+
- ./app
11+
- ./app hello world 1 2 3 4 5

examples/procfs/self/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"log"
5+
6+
"github.com/prometheus/procfs"
7+
)
8+
9+
func main() {
10+
log.SetFlags(0)
11+
12+
if err := run(); err != nil {
13+
log.Fatal(err)
14+
}
15+
}
16+
17+
func run() error {
18+
var (
19+
p procfs.Proc
20+
err error
21+
)
22+
p, err = procfs.Self()
23+
if err != nil {
24+
return err
25+
}
26+
27+
var (
28+
cmdline []string
29+
)
30+
cmdline, err = p.CmdLine()
31+
if err != nil {
32+
return err
33+
}
34+
35+
log.Printf("[Self] pid=%d, cmdline=%v", p.PID, cmdline)
36+
37+
return nil
38+
}

0 commit comments

Comments
 (0)