cron job不工作node-cron

时间:2022-02-11 08:04:40

I am trying to run a cron job after 10 minutes, sometimes it runs after 10 minutes and sometimes it runs after like 2 minutes when I call the webservice. Below is the code

我试图在10分钟后运行一个cron作业,有时它会在10分钟后运行,有时它会在2分钟后运行,当我调用web服务时。以下是代码

router.post('/getUser', function (req, res) {
    var task = cron.schedule('0 */10 * * * *', function () {
        console.log("cron job started")
     }, false);
     task.start();
})

It should always runs after 10 minutes not like sometime 2 minutes as soon as the webservice is called.

它应始终在10分钟后运行,而不是在调用webservice后的某个时间2分钟。

1 个解决方案

#1


1  

The cron syntax says to run the command at a fix time not after an interval.

cron语法表示在固定时间运行命令而不是在间隔之后。

The */10 means execute the command if the modulo is 0

* / 10表示如果模数为0则执行命令

In your case the code will be excecuted at second 0 of every 10 minutes at every hour at every day and so on.

在您的情况下,代码将在每天每小时每10分钟的第二个0开始执行,依此类推。

So your cron will be executed for instance at 09:00, 09:10, 09:20, 09:30 and so on.

所以你的cron将在例如09:00,09:10,09:20,09:30等执行。

The only way I know with build in methods is to use something like

我知道使用内置方法的唯一方法是使用类似的东西

setTimeout(myFunc, 10 * 60 * 1000);

An other option is to set a fixed cron running at the calculated correct time now +10 minutes with moment.js where you specify the exact execution time.

另一个选项是使用moment.js设置一个在计算的正确时间运行的固定cron,现在为+10分钟,您可以在其中指定确切的执行时间。

Example

var moment = require('moment')


router.post('/getUser', function (req, res) {
var cronString = moment().second() +' '+ moment().add(10,'minutes').minute() +' '+ moment().hour() +' '+ moment().day() +' '+ moment().month() +' *';

var task = cron.schedule(cronString, function () {
    console.log("cron job started")
 }, false);
 task.start();
})

But beware of the fact that this would be executed every year at the same time ;)

但请注意,这将每年同时执行;)

#1


1  

The cron syntax says to run the command at a fix time not after an interval.

cron语法表示在固定时间运行命令而不是在间隔之后。

The */10 means execute the command if the modulo is 0

* / 10表示如果模数为0则执行命令

In your case the code will be excecuted at second 0 of every 10 minutes at every hour at every day and so on.

在您的情况下,代码将在每天每小时每10分钟的第二个0开始执行,依此类推。

So your cron will be executed for instance at 09:00, 09:10, 09:20, 09:30 and so on.

所以你的cron将在例如09:00,09:10,09:20,09:30等执行。

The only way I know with build in methods is to use something like

我知道使用内置方法的唯一方法是使用类似的东西

setTimeout(myFunc, 10 * 60 * 1000);

An other option is to set a fixed cron running at the calculated correct time now +10 minutes with moment.js where you specify the exact execution time.

另一个选项是使用moment.js设置一个在计算的正确时间运行的固定cron,现在为+10分钟,您可以在其中指定确切的执行时间。

Example

var moment = require('moment')


router.post('/getUser', function (req, res) {
var cronString = moment().second() +' '+ moment().add(10,'minutes').minute() +' '+ moment().hour() +' '+ moment().day() +' '+ moment().month() +' *';

var task = cron.schedule(cronString, function () {
    console.log("cron job started")
 }, false);
 task.start();
})

But beware of the fact that this would be executed every year at the same time ;)

但请注意,这将每年同时执行;)