close interactive shell cmd on session restart

This commit is contained in:
sqshq 2019-06-09 10:37:05 -04:00
parent 988ef7de8e
commit 227dc81036
2 changed files with 7 additions and 3 deletions

View File

@ -25,7 +25,7 @@ type PtyInteractiveShell struct {
item *Item item *Item
variables []string variables []string
cmd *exec.Cmd cmd *exec.Cmd
File io.WriteCloser file io.WriteCloser
ch chan string ch chan string
errCount int errCount int
} }
@ -50,7 +50,7 @@ func (s *PtyInteractiveShell) init() error {
}() }()
s.cmd = cmd s.cmd = cmd
s.File = file s.file = file
s.ch = channel s.ch = channel
_, err = file.Read(make([]byte, 4096)) _, err = file.Read(make([]byte, 4096))
@ -65,10 +65,12 @@ func (s *PtyInteractiveShell) init() error {
func (s *PtyInteractiveShell) execute() (string, error) { func (s *PtyInteractiveShell) execute() (string, error) {
_, err := io.WriteString(s.File, fmt.Sprintf(" %s\n", s.item.sampleScript)) _, err := io.WriteString(s.file, fmt.Sprintf(" %s\n", s.item.sampleScript))
if err != nil { if err != nil {
s.errCount++ s.errCount++
if s.errCount > errorThreshold { if s.errCount > errorThreshold {
_ = s.cmd.Wait()
_ = s.file.Close()
s.item.ptyShell = nil // restart session s.item.ptyShell = nil // restart session
} }
return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err)) return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err))

View File

@ -24,6 +24,7 @@ func (s *BasicInteractiveShell) init() error {
cmd := exec.Command("sh", "-c", *s.item.initScript) cmd := exec.Command("sh", "-c", *s.item.initScript)
enrichEnvVariables(cmd, s.variables) enrichEnvVariables(cmd, s.variables)
cmd.Wait()
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
@ -72,6 +73,7 @@ func (s *BasicInteractiveShell) execute() (string, error) {
if err != nil { if err != nil {
s.errCount++ s.errCount++
if s.errCount > errorThreshold { if s.errCount > errorThreshold {
_ = s.cmd.Wait()
s.item.basicShell = nil // restart session s.item.basicShell = nil // restart session
} }
return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err)) return "", errors.New(fmt.Sprintf("Failed to execute command: %s", err))