1

The third command RunCommand("mysql_secure_installation"); doesn't show stdout/stderr buffers and command will not finish. Keyboard typing works but doesn't affect the process.

The mysql_secure_installation on ssh console works perfect.

Other commands work perfectly.

package main

import (
    "fmt"
    "os/exec"
    "os"
    "bufio"
)

func main() {
    RunCommand("lsb_release -a"); //works perfect
    RunCommand("apt-get update"); //works perfect
    RunCommand("mysql_secure_installation"); //empty output and waiting for something!
}

func RunCommand(command string) {
    args := []string {"-c", command}
    executer := exec.Command("sh", args...)

    stdout, err := executer.StdoutPipe()
    if err != nil {
        fmt.Print("Error creating STDOUT pipe")
        os.Exit(1)
    }

    stderr, err := executer.StderrPipe()
    if err != nil {
        fmt.Print("Error creating STDERR pipe")
        os.Exit(1)
    }

    stdoutScanner := bufio.NewScanner(stdout)
    stdoutScanner.Split(bufio.ScanLines)
    go func() {
        for stdoutScanner.Scan() {
            out := stdoutScanner.Text();
            fmt.Printf("%s\n", out)
        }
    }()

    stderrScanner := bufio.NewScanner(stderr)
    stderrScanner.Split(bufio.ScanLines)
    go func() {
        for stderrScanner.Scan() {
            error := stderrScanner.Text()
            fmt.Printf("%s\n", error)
        }
    }()

    err = executer.Start()
    if err != nil {
        os.Exit(1)
    }

    err = executer.Wait()
    if err != nil {
        os.Exit(1)
    }
}

UPDATE 1:

The main problem found and asked as new question in this link: How to store STDOUT buffer of `mysql_secure_installation` to a file

UPDATE 2:

CentOS and Debian tested and buffer works perfect but on my target os ( Ubuntu 16.04 LTS ) it doesn't work.

Behnam
  • 2,212
  • 2
  • 22
  • 32
  • also using `bufio.ScanRunes` as scanner not worked – Behnam Nov 02 '17 at 03:17
  • must be waiting for password? try commenting other two commands and just try to RunCommand for mysql_secure... – sahaj Nov 02 '17 at 06:50
  • @sahaj single running the command, desn't work again. also as mentioned typing with keyboard no affect the process ( with enter , ... ) – Behnam Nov 02 '17 at 11:44
  • I get the password prompt on go version go1.9.2 linux/amd64. – sahaj Nov 02 '17 at 12:37
  • @sahaj I use version 1.6.2, many thanks for reminding an update. I'll test it soon. – Behnam Nov 02 '17 at 12:40
  • @sahaj I tested version 1.9.2 but not worked and the problem persists. – Behnam Nov 03 '17 at 01:45
  • I found new problem that may be main reason to this question that asked in this link https://stackoverflow.com/q/47087442/1290995 – Behnam Nov 03 '17 at 02:15

1 Answers1

0

The reason it hangs is because it is still waiting for user input to complete.

As mentioned in the comments, mysql_secure_installation requires user input. If you want to run it without user input, you can try adding the --use-default argument.

If you want to wait for use input, then consider reading up on the difference between Run and Start. From the documentation:

Run starts the specified command and waits for it to complete.

Start starts the specified command but does not wait for it to complete.

You may need to experiment with using Run to allow user input to the program. Alternately, you could probably redirect another string into the command using stdin, but that might be more complicated than it's worth.

Addison
  • 7,322
  • 2
  • 39
  • 55
  • I wrote a large framework to do task like this, handling stdin exist as part of it worked perfecly with other process in non-interactive way. but I seperate code to simplify test without stdin and for presenting in stackoverflow. I need prompting for values but even the initial message doesn't show and no help when type and press enter character repeatly. Also I handled reading from STDIN to check if buffer presented in that but not help again. – Behnam Nov 02 '17 at 11:51
  • also no luck running with `Run` method that tested before asking question – Behnam Nov 02 '17 at 11:54