node.js 使用 Node-Cron 每 45 分钟运行一次 Cron Job
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24260462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Cron Job every 45 minutes with Node-Cron
提问by T. Rex
I'm using node-cron to run scheduled jobs. I want the jobs to run every 45 minutes, but its acting strangely
我正在使用 node-cron 来运行预定的作业。我希望这些工作每 45 分钟运行一次,但它的表现很奇怪
Here's the pattern I'm using
这是我正在使用的模式
'00 */45 * * * *'
'00 */45 * * * *'
I started my script at
Tue Jun 17 2014 08:17:39 GMT+0000 (GMT)
我开始了我的脚本
Tue Jun 17 2014 08:17:39 GMT+0000 (GMT)
Here's are the first couple of times the job was executed
这是作业执行的前几次
1. Tue Jun 17 2014 08:45:03 GMT+0000 (GMT)
1. Tue Jun 17 2014 08:45:03 GMT+0000 (GMT)
2. Tue Jun 17 2014 09:00:01 GMT+0000 (GMT)
2. Tue Jun 17 2014 09:00:01 GMT+0000 (GMT)
3. Tue Jun 17 2014 09:45:02 GMT+0000 (GMT)
3. Tue Jun 17 2014 09:45:02 GMT+0000 (GMT)
This is definitely not what I expected or want. All I want is to run the Jobs every 45 minutes. Can anyone help me with the pattern? Thanks :)
这绝对不是我所期望或想要的。我想要的只是每 45 分钟运行一次作业。谁能帮我看看这个模式?谢谢 :)
回答by Ben Fortune
回答by Keith Thompson
I'm more familiar with cron than with node-cron, but I've taken a quick look at the documentation.
我对 cron 比对node-cron更熟悉,但我快速浏览了文档。
If I understand it correctly, node-cron uses a syntax similar to that used by cron, but with an additional "seconds" field. So where a cron job might have:
如果我理解正确的话,node-cron 使用的语法类似于 cron 使用的语法,但有一个额外的“秒”字段。所以 cron 工作可能有:
# min hour mday month wday command
*/15 * * * * some-command
to schedule some-commandto run every 15 minutes, node-cron would use a similar syntax to specify the time to run:
要安排some-command每 15 分钟运行一次,node-cron 将使用类似的语法来指定运行时间:
'0 */15 * * * *'
(with an additional field to specify seconds), but it executes a specified JavaScript function, not an external command.
(带有一个额外的字段来指定秒),但它执行一个指定的 JavaScript 函数,而不是一个外部命令。
In standard cron, there is no syntax to specify running a job every 45 minutes. A specification of 0/45 * * * *would run a job twice each hour, at 0 and 45 minutes after the hour. To run a job every 45 minutes (at 00:00, 00:45, 01:30, 02:15, ..., i.e., 32 times per day) you'd have to schedule it to run every 15 minutes, and then invoke a script that checks the current time to decide whether to do anything.
在标准 cron 中,没有指定每 45 分钟运行一次作业的语法。一个规范0/45 * * * *每小时运行一次作业两次,分别在 0 分钟和 45 分钟后。要每 45 分钟运行一次作业(在 00:00、00:45、01:30、02:15,...,即每天 32 次),您必须将其安排为每 15 分钟运行一次,并且然后调用一个脚本来检查当前时间来决定是否做任何事情。
Or you can write an exhaustive list of all the times you want the job to run:
或者,您可以编写一份详尽的列表,列出您希望作业运行的所有时间:
0 0 * * * some-command
45 0 * * * some_command
30 1 * * * some_command
15 2 * * * some_command
# 28 lines omitted
I'd definitely want to write a script to generate this list.
我肯定想写一个脚本来生成这个列表。
(This is workable because 24 hours happens to be a multiple of 45 minutes. You couldn't run something every 35 minutes this way.)
(这是可行的,因为 24 小时恰好是 45 分钟的倍数。您不能以这种方式每 35 分钟运行一次。)
A similar approach should work for node-cron. Schedule the function to run every 15 minutes, and invoke a function that checks the current time to decide whether to run. For example, you can check whether the number of minutes since midnight modulo 45 is zero. (You might want to allow for a small variance in case the scheduling is not exact.)
类似的方法应该适用于 node-cron。安排函数每 15 分钟运行一次,并调用一个检查当前时间的函数来决定是否运行。例如,您可以检查自午夜取模 45 以来的分钟数是否为零。(如果计划不准确,您可能希望允许有一个小的差异。)
I don't know JavaScript well enough to suggest the best way to write this function, but it should be reasonably straightforward.
我不太了解 JavaScript,无法提出编写此函数的最佳方法,但它应该相当简单。
Or write 32 lines to specify all the times you want it to run.
或者写 32 行来指定您希望它运行的所有时间。
回答by Seff
There is no direct wayto do this. However, we can get the result by intercepting the schedule using a shell command within the target script.
有没有直接的方法来做到这一点。但是,我们可以通过在目标脚本中使用 shell 命令拦截计划来获得结果。
First, run the script at every 15 minutes:
首先,每 15 分钟运行一次脚本:
*/15 * * * * <target_script>
Then, within the target_script, place the following code before actual codes:
然后,在target_script 中,将以下代码放在实际代码之前:
#!/bin/sh
# Exit except at 0:45, 1:30, 2:15, 3:00, 3:45 etc
if ! echo "((`date +%-H`*60)+`date +%-M`)/45" | bc -l | grep "\.0*$" &> /dev/null;
then exit 1;
fi
# Your actual code goes here;
回答by Cody
You need to write a script as a wrapper to decide if the actual command shall be executed at every 45 minutes. That's 0, 45, 30 (= 45 + 45 - 60), 15 (= 30 + 45 - 60), 0 (= 15 + 45 - 60). so, the minutes to run the script shall be 0,15,30,45.
您需要编写一个脚本作为包装器来决定是否每 45 分钟执行一次实际命令。那是 0, 45, 30 (= 45 + 45 - 60), 15 (= 30 + 45 - 60), 0 (= 15 + 45 - 60)。因此,运行脚本的分钟数应为0,15,30,45.
The command date +%Mmay be helpful in the shell script.
该命令date +%M在 shell 脚本中可能会有所帮助。
回答by shakee93
you can use node-reelwhich is more readable, straight forward and awesome .
你可以使用node-reel更易读、更直接、更棒的。
const reel = require('node-reel')
reel().call(() => {
console.log(Date.now());
}).everyFortyFiveMinutes().run()
回答by SUBHASIS MONDAL
I tried this string for a 45-second interval and it works well:
我以 45 秒的间隔尝试了这个字符串,它运行良好:
'*/45 * * * * *'
回答by ryu
You can refer to cronr, it supports all the macro pattern and provides online demo cronr -- online demo
你可以参考cronr,它支持所有的宏模式并提供在线演示cronr -- online demo
回答by KARTHIKEYAN.A
use cron npm moduel something like this
使用像这样的 cron npm 模块
var cronJob = require('cron').CronJob;
var job = new cronJob({
cronTime:'0 */45 * * * *',
onTick: function(){
var my_date = new Date();
var tomorrow_date = my_date.getFullYear() + "-" + ('0'+(my_date.getMonth()+1)) + "-" + (my_date.getDate()+1)
var condition = [{},{$set: {'plannedDeliveryDate' :tomorrow_date +'T00:00:00.000Z'}}]
dbQuery.updateMany(orderModel, condition, function(err, result){
if(result.nModified == result.n) console.log(err, result)
})
},
start:true,
timeZone:'Asia/Kolkata'
});
job.start();

