在后台运行并收集数据的渠道

I am trying to run few functions periodically AND in the background because I am also serving a webservice.

import ("strings")
func run (cmd string, c chan []byte) {
    parts := strings.Fields(cmd)
    head := parts[0]
    parts = parts[1:len(parts)]

    out, err := exec.Command(head, parts...).Output()
        if err !=nil {
            log.Fatal(err)
        }
    c <-out
}

func main() {
      c:=make(chan []byte)
      go run ("date",c)
      output :=string(<-c)
}

I would like to run commands such as "date", "uptime", "ps" every few seconds and in the background The web service (net/http) will be running in the foreground outputing the results of these functions.

What is the best way to achieve this?

You should first find some lib or write some kind of utility that allow you to run continuosly a task, that actually your code is not doing.

For example: https://github.com/jasonlvhit/gocron

This is just an example, I do not know if this lib is fine or not.

Then you should choose the interface between your task and your web service.

You could run all in one, and use channels, one per task to comunicating with the go routines.

Or you can write text files with the data that your go rountines write and your web service read and process.

There are several other methods or solutions, this are just the easiest I could think about right now.

Hope this helps.