How to automatically run a scheduled task every hour in Node.js All In One
How to automatically run a scheduled task every hour in Node.js All In One
如何在 Node.js 中每间隔一小时自动运行一个
定时任务
引用场景
Node.js 后台爬虫服务,定时爬去指定页面,抓取最新数据,并写入到数据库中;
在同一个 Node.js 部署环境中,没有使用 Linux 的 crontab 权限,只能作为 express.js server 的一个子模块使用,故需要自动定时执行任务
- GitHub schedule
- crontab schedule
solutions
node-schedule
$ npm i node-schedule
https://www.npmjs.com/package/node-schedule
var schedule = require('node-schedule');
var task = schedule.scheduleJob('0 */8 * * *', function () {
console.log('Scheduled Task, every 8 hours');
});
var cron = require('node-schedule');
var rule = new cron.RecurrenceRule();
rule.hour = 8;
rule.minute = 0;
cron.scheduleJob(rule, function(){
console.log(new Date(), 'Every 8 hours');
});
https://github.com/node-schedule/node-schedule
node-cron
$ npm i -S node-cron
https://www.npmjs.com/package/node-cron
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
console.log('running a task every minute');
});
var cron = require('node-cron');
cron.schedule('0 8 * * *', () => {
console.log('Running a job at 08:00 at Asia/Shanghai timezone');
}, {
scheduled: true,
timezone: "Asia/Shanghai"
});
https://github.com/node-cron/node-cron
demos
特斯拉汽车
贷款利率计算器
,app 小程序
输入,贷款额,分期数,每月还款额,自动推算出,年利率,符合利率,总利率
https://github.com/web-full-stack/cyclic-express-server
特斯拉 比价
爬虫
https://www.cnblogs.com/xgqfrms/p/17349601.html
Web
Crawler
https://www.cnblogs.com/xgqfrms/p/17655286.html
/Users/xgqfrms-mm/Documents/github/node-web-framework-all-in-one/000-xyz/crawler/server.js
tools
0 */8 * * *
At minute 0 past every 8th hour.
refs
https://stackoverflow.com/search?q=how+to+run+a+schedule+task+in+Node.js
https://stackoverflow.com/questions/38503557/schedule-task-for-every-4-hours-in-node-js
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17673122.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-09-02 手把手的教你如何使用 Vite 搭建一个 Vue 3 UI 组件库 All In One
2022-09-02 GitHub Universe 2022 All In One
2022-09-02 how to use vanilla js iterate the Symbol Object All In One
2021-09-02 vue 单文件组件, 分离 UI 和 Template 和 js All In One
2021-09-02 线上环境 token 更新后自动推送到测试环境 All In One
2021-09-02 CSS inline-block & font-size bug All In One
2021-09-02 Snowpack All In One