package main import ( "bytes" "fmt" "io" "net/http" "os" "os/exec" "path" "path/filepath" "time" "linkfog.com/public/lib/l" ) func DownloadFile(file, url string) error { // URL of the file to be downloaded defer l.Info("exited download file") versionFile := path.Join(RootDir, file) // Send HTTP GET request response, err := HttpGet(url, nil) if err != nil { l.Error("get method failed:", err) return err } defer response.Body.Close() if response.StatusCode != http.StatusOK { return fmt.Errorf("url:%s, response status code: %d", url, response.StatusCode) } // Create the file out, err := os.Create(versionFile) if err != nil { l.Error("create new file failed:", err) return err } defer out.Close() // Write the body to file _, err = io.Copy(out, response.Body) if err != nil { l.Error("copy new file failed:", err) return err } l.Info("download new file success!!!") return nil } func HttpGet(url string, body []byte) (*http.Response, error) { client := http.Client{} req, _ := http.NewRequest("GET", url, bytes.NewBuffer(body)) //req.Header.Set("Content-Type", "application/json") return client.Do(req) } func startAgent() { for { agent := exec.Command(filepath.Join(RootDir, agentFileName), "-vendor=test") // 启动子进程 err := agent.Start() if err != nil { l.Errorf("agent start failed: %v", err) time.Sleep(10 * time.Second) continue } childPID = agent.Process.Pid // 等待子进程结束 err = agent.Wait() if err != nil { l.Errorf("agent wait failed: %v", err) } time.Sleep(10 * time.Second) } }