doudansui6650 2017-01-23 14:33
浏览 26

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

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?

  • 写回答

1条回答 默认 最新

  • doufenyu7610 2017-01-23 15:14
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line