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.