您当前的位置: 首页 > 

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

node-schedule.js实现crontab定时任务

彭世瑜 发布时间:2022-08-15 14:54:45 ,浏览量:3

文档

  • github https://github.com/node-schedule/node-schedule
  • npmjs https://www.npmjs.com/package/node-schedule

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)

安装

node -v
v16.14.0

pnpm install node-schedule

package.json

{
  "type": "module",
  "dependencies": {
    "node-schedule": "^2.1.0"
  }
}

示例:每5秒执行一次

import schedule from 'node-schedule'

// 秒 分 时 日 月 周
const job = schedule.scheduleJob('*/5 * * * * *', function () {
  console.log(new Date());
})

运行结果

$ node demo.js

2022-08-15T06:45:05.009Z
2022-08-15T06:45:10.008Z
2022-08-15T06:45:15.004Z
2022-08-15T06:45:20.004Z
关注
打赏
1665367115
查看更多评论
立即登录/注册

微信扫码登录

0.3001s