Skip to content

Windows - Process keeps running when stdin is set #219

@powellnorma

Description

@powellnorma

The following works fine:

func runPsCmd(psCmd string) {
	pthPs := `C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe`

	cmd := exec.Command(pthPs, "-Command", psCmd)

	cmd.Stderr = os.Stderr
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin

	err := cmd.Start()
	if err != nil {
		panic(err)
	}

	err = cmd.Wait()
	if err != nil {
		exitError, ok := err.(*exec.ExitError)
		if ok {
			os.Exit(exitError.ExitCode())
		} else {
			panic(err)
		}
	}
	os.Exit(0)
}

func main() {
	runPsCmd("echo myTest")
}

However when doing the same but with:

func runPsCmd(s ssh.Session, psCmd string) {
	// [..]
	cmd.Stderr = s
	cmd.Stdout = s
	cmd.Stdin = s
	// [..]
}

cmd.Wait() blocks until I pressed Enter once. When I don't assign cmd.Stdin at all, it works as expected, but some programs may require user input. What could this by caused by? And how can this be fixed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions