一个cron作业可以每隔'x'秒运行一次

dee*_*ABC 6 cron node.js

我有一个cron作业设置,最小值为60秒,我希望程序能够以第二个间隔运行,即无论我将其设置为60秒以后.

因此,例如,我希望cron作业每65秒运行一次,或者每63秒运行一次,或者每160秒运行一次.

这可能吗?或者cron作业是否设置为每隔60秒运行一次?

如果是的话,这个cron的工作是什么样的?

我设法让它每隔'x'分钟运行一次: 到目前为止,我已经设法使用用户插入的数据库值来每隔几分钟运行一次cron作业.看起来像这样:

  cron.schedule('*/' + test + ' * * * *', function () {
    console.log('running a task every minute');
  });
Run Code Online (Sandbox Code Playgroud)

架构看起来像:

var CronSchema = new mongoose.Schema({
  minutes: { type: Number, min: 1 }
})
Run Code Online (Sandbox Code Playgroud)

如何每隔'x'秒运行一次cron作业? 理想情况下,我只允许用户以秒为单位输入值,因此如果他们希望每两分钟运行一次,则必须输入120秒.

ade*_*neo 13

如果您正在使用Node的中间件,您也可以添加秒

cron.schedule('*/' + test + '0,30 * * * * *', function () {
    console.log('running a task every minute');
});
Run Code Online (Sandbox Code Playgroud)

将每30秒运行一次,即on xx:00:000xx:30:000

请注意,javascript版本有六个值,而不是五个像本机Linux Cron,这意味着第六个是秒,这将是第一个

*    *    *    *    *    *
?    ?    ?    ?    ?    ?
?    ?    ?    ?    ?    |
?    ?    ?    ?    ?    ? day of week (0 - 7) (0 or 7 is Sun)
?    ?    ?    ?    ?????? month (1 - 12)
?    ?    ?    ??????????? day of month (1 - 31)
?    ?    ???????????????? hour (0 - 23)
?    ????????????????????? minute (0 - 59)
?????????????????????????? second (0 - 59, OPTIONAL)
Run Code Online (Sandbox Code Playgroud)

有关允许值的完整文档可以在crontab文档中找到