Skip to content

Commit

Permalink
support windows
Browse files Browse the repository at this point in the history
  • Loading branch information
makiuchi-d committed May 17, 2020
1 parent 86f8e7c commit 41728bd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
6 changes: 2 additions & 4 deletions arelo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"path"
"strings"
Expand Down Expand Up @@ -317,10 +316,9 @@ func runner(ctx context.Context, wg *sync.WaitGroup, cmd []string, delay time.Du
}

func runCmd(ctx context.Context, cmd []string) error {
c := exec.Command(cmd[0], cmd[1:]...)
c := prepareCommand(cmd)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
if err := c.Start(); err != nil {
return err
}
Expand All @@ -331,7 +329,7 @@ func runCmd(ctx context.Context, cmd []string) error {

select {
case <-ctx.Done():
err := syscall.Kill(-c.Process.Pid, syscall.SIGTERM)
err := killChilds(c)
if err != nil {
return xerrors.Errorf("kill error: %w", err)
}
Expand Down
18 changes: 18 additions & 0 deletions cmd_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// +build !windows

package main

import (
"os/exec"
"syscall"
)

func prepareCommand(cmd []string) *exec.Cmd {
c := exec.Command(cmd[0], cmd[1:]...)
c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
return c
}

func killChilds(c *exec.Cmd) error {
return syscall.Kill(-c.Process.Pid, syscall.SIGTERM)
}
19 changes: 19 additions & 0 deletions cmd_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// +build windows

package main

import (
"os/exec"
"strconv"
)

func prepareCommand(cmd []string) *exec.Cmd {
return exec.Command(cmd[0], cmd[1:]...)
}

func killChilds(c *exec.Cmd) error {
kill := exec.Command("TASKKILL", "/T", "/F", "/PID", strconv.Itoa(c.Process.Pid))
kill.Stderr = c.Stderr
kill.Stdout = c.Stdout
return kill.Run()
}

0 comments on commit 41728bd

Please sign in to comment.