golang+read_file+call_shell+goroutine
2021-06-29 14:33 divl 阅读(30) 评论(0) 编辑 收藏 举报package main import ( "bufio" "bytes" "fmt" "os" "os/exec" "sync" ) //call shell func call_shell(script_name string, script_args string) (string, string, error) { var script_stdout bytes.Buffer var script_stderr bytes.Buffer cmd := exec.Command("/bin/bash", script_name, script_args) cmd.Stdout = &script_stdout cmd.Stderr = &script_stderr err := cmd.Run() return script_stdout.String(), script_stderr.String(), err } func main() { var wg sync.WaitGroup //Open file file_path, _ := os.Getwd() file, err := os.Open(file_path + "/db_list") if err != nil { fmt.Print("Open file error") } defer file.Close() //Read file line by line scanner := bufio.NewScanner(file) for scanner.Scan() { wg.Add(1) go func(db_name string) { defer wg.Done() shell_path := "/opt/go_workspace/script/auto_dump.sh" script_stdout, script_stderr, _ := call_shell(shell_path, db_name) fmt.Printf("Dump db log for %s\n%s\n%s\n", db_name, script_stdout, script_stderr) }(scanner.Text()) } wg.Wait() }