我想https://github.com/robfig/cron每天中午 12:05 使用這個 crontab 庫執行函數。這是我當前的代碼:cronHandler.AddFunc("@midnight", func() {
fmt.Println("crontab ping")
}我如何每天凌晨 03:00,時區 +2 使用 crontab 執行我的功能?我的問題是當前函數使用我的服務器的時區,第二個問題是這個庫不允許在特定的特定時間執行。我該怎么做?
1 回答

繁星coding
TA貢獻1797條經驗 獲得超4個贊
這可以通過cron庫以及對代碼的一些小調整來完成。
一些東西:
通過在時區數據庫的列表中找到您選擇的時區
將此時區加載到
time.LoadLocation
或使用該time.FixedZone
方法使用自定義位置方法初始化 Cron 作業運行器的新實例
NewWithLocation
編輯方法的第一個參數
.AddFunc
,以添加更細化的 cron 計劃,將通用替換@midnight
為更具體的5 0 * * * *
表示每天上午 12:05,或替換為您想要的 cron 執行時間在新的自定義位置
完整示例如下:
// pass in your specific zone name, using USA/LA as example
customLocation := time.LoadLocation("America/Los_Angeles")
// initialize new cron job runner with custom location
cronHandler := cron.NewWithLocation(customLocation)
// the 0/24th hour and 5th minute of every day
cronHandler.AddFunc("5 0 * * * *", func() {
? ? fmt.Println("crontab ping")
})
- 1 回答
- 0 關注
- 248 瀏覽
添加回答
舉報
0/150
提交
取消