forked from ddollar/forego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
darwin.go
48 lines (44 loc) · 1.08 KB
/
darwin.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"syscall"
"time"
)
func (p *Process) Start() {
command := []string{"/bin/bash", p.shellArgument(), fmt.Sprintf("source \"%s\" 2>/dev/null; %s", filepath.Join(p.Root, ".profile"), p.Command)}
p.cmd = exec.Command(command[0], command[1:]...)
p.cmd.Dir = p.Root
p.cmd.Env = p.envAsArray()
p.cmd.Stdin = p.Stdin
p.cmd.Stdout = p.Stdout
p.cmd.Stderr = p.Stderr
if !p.Interactive {
p.cmd.SysProcAttr = &syscall.SysProcAttr{}
p.cmd.SysProcAttr.Setsid = true
}
p.cmd.Start()
}
func (p *Process) Signal(signal syscall.Signal) {
if p.Running() {
group, _ := os.FindProcess(-1 * p.Pid())
group.Signal(signal)
}
}
func ShutdownProcesses(of *OutletFactory) {
shutdown_mutex.Lock()
of.SystemOutput("shutting down")
for name, ps := range processes {
of.SystemOutput(fmt.Sprintf("sending SIGTERM to %s", name))
ps.Signal(syscall.SIGTERM)
}
go func() {
time.Sleep(shutdownGraceTime)
for name, ps := range processes {
of.SystemOutput(fmt.Sprintf("sending SIGKILL to %s", name))
ps.Signal(syscall.SIGKILL)
}
}()
}