Skip to content

Commit 18d12f6

Browse files
committed
+ add usage
1 parent b114aee commit 18d12f6

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
11
# process-monitor
22
process-monitor is a Go language library for observing the life cycle of system processes.
3+
4+
## Usage
5+
6+
```go
7+
package main
8+
9+
import (
10+
"fmt"
11+
"os"
12+
"os/signal"
13+
"syscall"
14+
15+
processwatcher "github.com/merbridge/process-watcher"
16+
)
17+
18+
func main() {
19+
w := processwatcher.NewProcessWatcher()
20+
if err := w.Start(); err != nil {
21+
panic(err)
22+
}
23+
sigs := make(chan os.Signal, 1)
24+
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
25+
for {
26+
select {
27+
case e := <-w.Events():
28+
if e.Err != nil {
29+
panic(e.Err)
30+
}
31+
typ := "not-support"
32+
var obj interface{}
33+
switch e.GetType() {
34+
case processwatcher.PROC_EVENT_EXEC:
35+
typ = "exec"
36+
obj = e.GetExec()
37+
case processwatcher.PROC_EVENT_FORK:
38+
typ = "fork"
39+
obj = e.GetFork()
40+
case processwatcher.PROC_EVENT_EXIT:
41+
typ = "exit"
42+
obj = e.GetExit()
43+
}
44+
fmt.Printf("%s: %+v\n", typ, obj)
45+
case <-sigs:
46+
return
47+
}
48+
}
49+
}
50+
```
51+
52+
> Nit: Requires a root user to run, you can run as: `go run -exec sudo ./app`

0 commit comments

Comments
 (0)