From 892d809a1c884c81c72f7f7e073de67437cfebb8 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Wed, 18 Jul 2018 16:08:50 -0700 Subject: [PATCH] update kill command on soldier --- aws-experiment-launch/experiment/soldier/main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/aws-experiment-launch/experiment/soldier/main.go b/aws-experiment-launch/experiment/soldier/main.go index cc6e8b8b6..aaaef40b2 100644 --- a/aws-experiment-launch/experiment/soldier/main.go +++ b/aws-experiment-launch/experiment/soldier/main.go @@ -16,6 +16,7 @@ import ( "os" "os/exec" "path/filepath" + "runtime" "strings" ) @@ -150,10 +151,20 @@ func handleInitCommand(args []string, w *bufio.Writer) { func handleKillCommand(w *bufio.Writer) { log.Println("Kill command") - runCmd("./kill_node.sh") + killPort(setting.port) logAndReply(w, "Kill command done.") } +func killPort(port string) { + if runtime.GOOS == "windows" { + command := fmt.Sprintf("(Get-NetTCPConnection -LocalPort %s).OwningProcess -Force", port) + runCmd("Stop-Process", "-Id", command) + } else { + command := fmt.Sprintf("lsof -i tcp:%s | grep LISTEN | awk '{print $2}' | xargs kill -9", port) + runCmd("bash", "-c", command) + } +} + func handlePingCommand(w *bufio.Writer) { log.Println("Ping command") logAndReply(w, "I'm alive")