我在设置javascript cron时遇到问题

时间:2019-07-31 14:48:09

标签: javascript node.js cron

我需要帮助设置执行时间。我有这个:

cron.schedule('1,2,4,5 * * * *', () => {
  console.log('running every minute 1, 2, 4 and 5');
});

而是每隔1,2,3,4,5分钟执行一次,我想执行每小时247的每一分钟

1 个答案:

答案 0 :(得分:1)

我假设您正在使用node-cron

node-cron的cron语法如下:

  ┌────────────── second (optional)
  │ ┌──────────── minute
  │ │ ┌────────── hour
  │ │ │ ┌──────── day of month
  │ │ │ │ ┌────── month
  │ │ │ │ │ ┌──── day of week
  │ │ │ │ │ │
  │ │ │ │ │ │
  * * * * * *

因此,如果您想每分钟安排一次任务,则可以通过以下方式之一编写它:

// schedule to run every minute (at 0 seconds on the clock)
// e.g. this will run at 00:00:00, 00:01:00, 00:02:00, ... (hh:mm:ss)
cron.schedule('* * * * *', () => {
  console.log('this will run every minute');
});

// same as above, but with explicit 0 seconds
cron.schedule('0 * * * * *', () => {
  console.log('this will also run every minute');
});

// run every minute, but at the 30 second mark
// e.g. this will run at 00:00:30, 00:01:30, 00:02:30, ... (hh:mm:ss)
cron.schedule('30 * * * * *', () => {
  console.log('this will also run every minute (when seconds hits 30)');
});