如何使cron在10分钟的间隔内仅运行42次?

问题描述

我需要使作业每10分钟运行42次。我将使用具有以下cron语法的AWS Scheduled Task进行此操作:https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html

我想出的基本算术公式:

const arr = [{ "id": 5,"title": "buttercup","postContent": "dsdsfsfsfsfsfs","likedByMe": false,"likeCounts": 0,"userId": 1,"createdAt": "2020-08-17T03:41:16.749Z","updatedAt": "2020-08-17T03:41:16.749Z","author": { "username": "blankman","gravatar": "https://lh3.googleusercontent.com/a-/AOh14GiZAw_0UZmt7X0pJFSIv6ELQMd2nN41v0a-eKrN","bio": null },"Likes": [],"Comments": [{ "id": 46,"comment_body": "fsfsfsfsfsf","gifUrl": "","postId": 5,"createdAt": "2020-08-18T04:46:08.946Z","updatedAt": "2020-08-18T04:46:08.946Z","commentReplies": [{ "id": 18,"replyBody": "fsfsffsffsffsf","userId": 2,"commentId": 46,"createdAt": "2020-08-21T16:40:47.205Z","updatedAt": "2020-08-21T16:40:47.205Z","author": { "username": "barNowl","gravatar": null,"bio": null } }],"author": { "username": "blankman2","bio": null } },{ "id": 47,"author": { "username": "barNowl2","author": { "username": "blankman3",],"RePosts": [],"RepostedByMe": false }];
const res = arr.flatMap(x => [x.author.username].concat(x.Comments.flatMap(y => y.commentReplies.map(z => z.author.username).concat(y.author.username))));
console.log(res);

这意味着我希望计划任务在7个小时内每隔10分钟运行一次。一旦达到7个小时,它将在一天的剩余时间内不再运行。

鉴于此,我想出了这个表达式?

42 * 10 minutes = 420 minutes / 60 minutes = 7 hours.

这种表达方式对我的需求是否准确且最有效?

解决方法

表达式的语法实际上应为cron(1/10 15-21 * * ? *),因此您要删除两者之间的逗号。

您已将表达式的分钟部分偏移了1,因此它将在1,11,21,31,41,51处运行。如果您只是想让它每10分钟运行一次,则可以改为*/10

这将在一个小时的7小时内运行6次,这样一来,您的计数就达到了42,这是您期望的值,范围是15:0121:51

请注意,这是在UTC中执行的,因此请确保时间与您期望它运行的任何时区相匹配。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...