每45分钟用Node-Cron运行Cron

时间:2022-01-27 05:17:42

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)

我的剧本开始于2014年6月17日星期二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。2014年06月17日08:45:03 GMT+0000 (GMT)

2. Tue Jun 17 2014 09:00:01 GMT+0000 (GMT)

2。2014年6月17日09:00:01 GMT+0000 (GMT)

3. Tue Jun 17 2014 09:45:02 GMT+0000 (GMT)

3所示。2014年6月17日格林尼治时间+0000(格林尼治时间)

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分钟做一次。谁能帮我弄一下这个图案吗?谢谢:)

8 个解决方案

#1


32  

You're probably looking for

你可能找的

0 */45 * * * *

The ranges are here.

范围都在这里。

  • Seconds: 0-59
  • 秒:0-59
  • Minutes: 0-59
  • 分钟:0-59
  • Hours: 0-23
  • 小时:0-23
  • Day of Month: 1-31
  • 月日:日至31日
  • Months: 0-11
  • 个月:划分的
  • Day of Week: 0-6
  • 天的一周:0 - 6

#2


10  

I'm more familiar with cron than with node-cron, but I've taken a quick look at the documentation.

与node-cron相比,我更熟悉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使用的语法类似,但是附加了一个“seconds”字段。因此,cron的工作可能是:

# min hour mday month wday command
*/15  *    *    *     *    some-command

to schedule some-command to run every 15 minutes, node-cron would use a similar syntax to specify the time to run:

要安排每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,……),即你必须安排它每15分钟运行一次,然后调用一个脚本检查当前的时间,以决定是否要做什么。

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,无法给出编写此函数的最佳方式,但它应该相当简单。

#3


1  

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 +%M may be helpful in the shell script.

命令日期+%M在shell脚本中可能很有用。

#4


1  

There is no direct way to 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;

#5


0  

use cron npm moduel something like this

使用cron npm moduel之类的东西

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();

#6


0  

I have tried with this for 45 seconds interval and working fine

我已经尝试了45秒间隔和工作良好

'*/45 * * * * *'

'*/45 * * * * *'

#7


0  

You can refer to cronr, it supports all the macro pattern and provides online demo cronr -- online demo

您可以参考cronr,它支持所有的宏模式,并提供在线演示cronr——在线演示

#8


-3  

Just try this,

试试这个,

0-23/45 * * * *

#1


32  

You're probably looking for

你可能找的

0 */45 * * * *

The ranges are here.

范围都在这里。

  • Seconds: 0-59
  • 秒:0-59
  • Minutes: 0-59
  • 分钟:0-59
  • Hours: 0-23
  • 小时:0-23
  • Day of Month: 1-31
  • 月日:日至31日
  • Months: 0-11
  • 个月:划分的
  • Day of Week: 0-6
  • 天的一周:0 - 6

#2


10  

I'm more familiar with cron than with node-cron, but I've taken a quick look at the documentation.

与node-cron相比,我更熟悉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使用的语法类似,但是附加了一个“seconds”字段。因此,cron的工作可能是:

# min hour mday month wday command
*/15  *    *    *     *    some-command

to schedule some-command to run every 15 minutes, node-cron would use a similar syntax to specify the time to run:

要安排每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,……),即你必须安排它每15分钟运行一次,然后调用一个脚本检查当前的时间,以决定是否要做什么。

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,无法给出编写此函数的最佳方式,但它应该相当简单。

#3


1  

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 +%M may be helpful in the shell script.

命令日期+%M在shell脚本中可能很有用。

#4


1  

There is no direct way to 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;

#5


0  

use cron npm moduel something like this

使用cron npm moduel之类的东西

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();

#6


0  

I have tried with this for 45 seconds interval and working fine

我已经尝试了45秒间隔和工作良好

'*/45 * * * * *'

'*/45 * * * * *'

#7


0  

You can refer to cronr, it supports all the macro pattern and provides online demo cronr -- online demo

您可以参考cronr,它支持所有的宏模式,并提供在线演示cronr——在线演示

#8


-3  

Just try this,

试试这个,

0-23/45 * * * *