doujiang5211 2017-04-10 02:18
浏览 214
已采纳

GO中的CRON作业未按预期运行

Here is my code:

package main

import (
    "fmt"
    "github.com/robfig/cron"
)

func main() {
    c := cron.New()
    c.AddFunc("@every 3m", func() { fmt.Println("Every 3 min") })
    c.Start()
    fmt.Println("Done")
}

The problem is when I run the code using go run it just prints Done then exits. I am just trying to print the function every 3 minute.

  • 写回答

1条回答 默认 最新

  • doqw89029 2017-04-10 02:51
    关注

    Your code does this:

    1. Initiates a new cron instance:

      c := cron.New()
      
    2. Adds a cron job:

      c.AddFunc("@every 3m", func() { fmt.Println("Every 3 min") })
      
    3. Starts the cron instance in a new goroutine (in the background):

      c.Start()
      
    4. Prints "Done":

      fmt.Println("Done")
      
    5. Then exits.

    If you want your program to keep running, you need to make it do something that keeps it running. If there is nothing else you need your program to do, just have it wait for something to finish that never finishes. See this answer for some suggestions along those lines.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘