Skip to content

Commit

Permalink
feature: add signal flag in killing process expriment
Browse files Browse the repository at this point in the history
Signed-off-by: xcaspar <x.caspar@gmail.com>
  • Loading branch information
xcaspar authored and RinaisSuper committed May 26, 2020
1 parent 0512f43 commit 7c64155
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions exec/bin/killprocess/killprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@ import (
"github.com/chaosblade-io/chaosblade-exec-os/exec/bin"
)

var killProcessName, killProcessInCmd, killProcessLocalPorts string
var killProcessName, killProcessInCmd, killProcessLocalPorts, killProcessSignal string
var killProcessCount int

func main() {
flag.StringVar(&killProcessName, "process", "", "process name")
flag.StringVar(&killProcessInCmd, "process-cmd", "", "process in command")
flag.IntVar(&killProcessCount, "count", 0, "limit count")
flag.StringVar(&killProcessLocalPorts, "local-port", "", "local service ports")
flag.StringVar(&killProcessSignal, "signal", "9", "kill process signal")
bin.ParseFlagAndInitLog()

killProcess(killProcessName, killProcessInCmd, killProcessLocalPorts, killProcessCount)
killProcess(killProcessName, killProcessInCmd, killProcessLocalPorts, killProcessSignal, killProcessCount)
}

var cl = channel.NewLocalChannel()

func killProcess(process, processCmd, localPorts string, count int) {
func killProcess(process, processCmd, localPorts, signal string, count int) {
var pids []string
var err error
var ctx = context.WithValue(context.Background(), channel.ExcludeProcessKey, "blade")
Expand Down Expand Up @@ -78,7 +79,7 @@ func killProcess(process, processCmd, localPorts string, count int) {
if count > 0 && len(pids) > count {
pids = pids[:count]
}
response := cl.Run(ctx, "kill", fmt.Sprintf("-9 %s", strings.Join(pids, " ")))
response := cl.Run(ctx, "kill", fmt.Sprintf("-%s %s", signal, strings.Join(pids, " ")))
if !response.Success {
bin.PrintErrAndExit(response.Err)
return
Expand Down
8 changes: 8 additions & 0 deletions exec/process_kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func NewKillProcessActionCommandSpec() spec.ExpActionCommandSpec {
Name: "local-port",
Desc: "Local service ports. Separate multiple ports with commas (,) or connector representing ranges, for example: 80,8000-8080",
},
&spec.ExpFlag{
Name: "signal",
Desc: "Killing process signal, such as 9,15",
},
},
ActionFlags: []spec.ExpFlagSpec{},
ActionExecutor: &KillProcessExecutor{},
Expand Down Expand Up @@ -98,6 +102,7 @@ func (kpe *KillProcessExecutor) Exec(uid string, ctx context.Context, model *spe
process := model.ActionFlags["process"]
processCmd := model.ActionFlags["process-cmd"]
localPorts := model.ActionFlags["local-port"]
signal := model.ActionFlags["signal"]
if process == "" && processCmd == "" && localPorts == "" {
return spec.ReturnFail(spec.Code[spec.IllegalParameters], "less process matcher")
}
Expand All @@ -116,6 +121,9 @@ func (kpe *KillProcessExecutor) Exec(uid string, ctx context.Context, model *spe
} else if localPorts != "" {
flags = fmt.Sprintf(`%s --local-port "%s"`, flags, localPorts)
}
if signal != "" {
flags = fmt.Sprintf(`%s --signal %s`, flags, signal)
}
return kpe.channel.Run(ctx, path.Join(kpe.channel.GetScriptPath(), killProcessBin), flags)
}

Expand Down

0 comments on commit 7c64155

Please sign in to comment.