-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.go
99 lines (74 loc) · 1.95 KB
/
loader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"os/signal"
"syscall"
)
type TestProcess struct {
Process *exec.Cmd
Stdout *bufio.Writer
Stderr *bufio.Writer
}
const RMLOGS_EXEC = "./remove_logs.sh"
const EXEC = "./test"
const NUM_PROCESSES = 5
func main() {
// Remove logs by using shell script.
rmLogs := exec.Command(RMLOGS_EXEC)
err := rmLogs.Run()
if err != nil {
fmt.Println("Failed to remove logs via shell script.")
fmt.Println(err)
}
var processes []*TestProcess
for i := 0; i < NUM_PROCESSES; i++ {
process := exec.Command(EXEC)
stdoutPipe, _ := process.StdoutPipe()
stderrPipe, _ := process.StderrPipe()
err := process.Start()
if err != nil {
fmt.Printf("[%d] Error creating process.\n", i)
fmt.Println(err)
continue
}
if process.Process == nil {
fmt.Printf("[%d] Could not find process.\n", process.Process.Pid)
continue
}
// Create log file.
fileName := fmt.Sprintf("logs/%d.log", process.Process.Pid)
logFile, err := os.Create(fileName)
if err != nil {
fmt.Printf("[%d] Error creating log file :: %s.\n", process.Process.Pid, fileName)
continue
}
stdoutWriter := bufio.NewWriter(logFile)
stderrWriter := bufio.NewWriter(logFile)
// Create goroutines to capture stdout and stderr output and write them our log files.
go func() {
scanner := bufio.NewScanner(stdoutPipe)
for scanner.Scan() {
line := scanner.Text()
stdoutWriter.WriteString(line + "\n")
stdoutWriter.Flush()
}
}()
go func() {
scanner := bufio.NewScanner(stderrPipe)
for scanner.Scan() {
line := scanner.Text()
stderrWriter.WriteString(line + "\n")
stderrWriter.Flush()
}
}()
processes = append(processes, &TestProcess{Process: process, Stdout: stdoutWriter, Stderr: stderrWriter})
}
fmt.Println("Started loader. Check logs/ directory for outputs.")
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
<-sigc
os.Exit(0)
}