dongqu2863 2017-08-29 14:52
浏览 128

是否可以在特定日期/时间调用cron作业?

I am using what seems to be the most popular cron package by robfig: https://godoc.org/github.com/robfig/cron. Currently I know that I can invoke an hourly cron job with:

c.AddFunc("@hourly", func() { fmt.Println("Every hour") })

However I wonder if it is possible to set it so that it only starts after (for example) Sep 1st, 2017? If it's not possible using that package, how else can I achieve that? Thanks.

  • 写回答

2条回答 默认 最新

  • douxin0251 2017-08-29 18:43
    关注

    Adding to @Adrians Comments,

    The package robfig/cron supports cron expression format. To have the cron run 30 mins past every hour viz on 9:30 am,10:30 am, 11:30 am. You could use

    c.AddFunc("0 30 * * * *", func() {})

    an implementation to have the cron run after Sep 1, 2017

    package main
    
    import (
        "fmt"
        "time"
        "github.com/robfig/cron"
    )
    
    func main() {
    
        // time set to Sep 1, 2017 00:00 Hours UTC
        t := time.Date(2017, time.September, 1, 0, 0, 0, 0, time.UTC) 
    
        c := cron.New()
    
        //using cron expression format to have the function run every hour on the half hour
        c.AddFunc("0 30 * * * *", func() {      
    
            if time.Now.After(t){
            fmt.Println("yay")
            //insert logic block
            } 
         })
    
    }
    

    Note: The cron expression format used by this package is different from the standard unix cron format. The expression format used by this package allows for second precision unlike unix cron format.

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据