File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11# process-monitor
22process-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 `
You can’t perform that action at this time.
0 commit comments