返回

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

发布时间:2022-09-16 18:14:13 324
# npm# json# git# github

文档

  • 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

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

 

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
精选文章
thumb 中国研究员首次曝光美国国安局顶级后门—“方程式组织”
thumb 俄乌线上战争,网络攻击弥漫着数字硝烟
thumb 从网络安全角度了解俄罗斯入侵乌克兰的相关事件时间线