nodejs定时任务node-schedule
1:使用npm安装node-schedule模块
npm install node-schedule
(1)每隔5分钟执行一次:
var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.minute = [0,5,10,15,20,25,30,35,40,45,50,55];
var j = schedule.scheduleJob(rule,function(){
console.log("执行任务:"+new Date());
});
(2)上午8点到晚上20点每隔5分钟执行一次:
var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.hour = [8,9,10,11,12,13,14,15,16,17,18,19,20];
rule.minute = [0,5,10,15,20,25,30,35,40,45,50,55];
var j = schedule.scheduleJob(rule,function(){
console.log("执行任务:"+new Date());
});
2:以下内容参考:
http://www.codexpedia.com/javascript/nodejs-cron-schedule-examples/
Using the node-schedule to schedule a job to run at a specific time on a specific date. As the first example, the node-schedule module is imported and save it in the variable cron. In the following examle, the require statement will be ommitted and this variable cron will be used.
|
1
2
3
4
5
6
|
var cron = require('node-schedule');/* run the job at 18:55:30 on Dec. 14 2018*/var date = new Date(2018, 11, 14, 18, 56, 30);cron.scheduleJob(date, function(){ console.log(new Date(), "The world is going to end today."); }); |
Schedule a recurring job using the RecurrenceRule, example 1.
|
1
2
3
4
5
|
var rule = new cron.RecurrenceRule();rule.second = 30;cron.scheduleJob(rule, function(){ console.log(new Date(), 'The 30th second of the minute.');}); |
Schedule a recurring job using the RecurrenceRule, example 2.
|
1
2
3
4
5
6
7
8
|
/* This runs at 3:10AM every Friday, Saturday and Sunday. */var rule2 = new cron.RecurrenceRule();rule2.dayOfWeek = [5,6,0];rule2.hour = 3;rule2.minute = 10;cron.scheduleJob(rule2, function(){ console.log('This runs at 3:10AM every Friday, Saturday and Sunday.');}); |
Specify the schedule as an object literal.
|
1
2
3
4
|
/* This runs at 2:30AM on every Sunday */cron.scheduleJob({hour: 2, minute: 30, dayOfWeek: 0}, function(){ console.log('This runs at 2:30AM on every Sunday');}); |
Specify the schedule in unix cron syntax.
|
1
2
3
4
|
/* This runs at the 30th mintue of every hour. */cron.scheduleJob('30 * * * * *', function(){ console.log('This runs at the 30th mintue of every hour.');}); |
nodejs定时任务node-schedule的更多相关文章
- 基于agenda的Nodejs定时任务管理框架搭建
0.背景 在大型项目中,定时任务的应用场景越来越广.一般来说,按照微服务的思想,我们会将定时任务单独部署一套服务,核心的业务接口独立到另一个服务中,从而降低相互之间的耦合程度.在需要使用定时任务时,只 ...
- NodeJS定时任务
在实际开发项目中,会遇到很多定时任务的工作.比如:定时导出某些数据.定时发送消息或邮件给用户.定时备份什么类型的文件等等 一般可以写个定时器,来完成相应的需求,在node.js中自已实现也非常容易,接 ...
- NodeJS(node.exe, npm, express, live-server)安装
1.下载node.exe 下载https://nodejs.org/en/download/current/ 创建D:\GreenSoftware\NodeJS目录,并将node.exe放到目录中. ...
- [Nodejs] 用node写个爬虫
寻找爬取的目标 首先我们需要一个坚定的目标,于是找个一个比较好看一些网站,将一些信息统计一下,比如 url/tag/title/number...等信息 init(1, 2); //设置页数,现在是1 ...
- 【NodeJS】Node.JS 开发环境安装
1.前言 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O ...
- Nodejs:Node.js模块机制小结
今天读了<深入浅出Nodejs>的第二章:模块机制.现在做一个简单的小结. 序:模块机制大致从这几个部分来讲:JS模块机制的由来.CommonJS AMD CMD.Node模块机制和包和n ...
- Nodejs 定时任务
安装扩展:node-schedule npm install node-schedule 1.linux Crontab风格 var schedule = require('node-schedule ...
- SpringBoot系列:Spring Boot定时任务Spring Schedule
Spring Schedule是Spring提供的定时任务框架,相较于Quartz,Schedule更加简单易用,在中小型应用中,对于大部分需求,Schedule都可以胜任. 一.Spring Sch ...
- 定时任务模块 schedule
# coding:utf-8 from learning_python.Telegram_push.check_hardware import check_cpu import schedule im ...
随机推荐
- [Java Concurrent] 多线程合作 wait / notifyAll 的简单案例
本案例描述的是,给一辆汽车打蜡.抛光的场景. Car 是一辆被打蜡抛光的汽车,扮演共享资源的角色. WaxOnCommand 负责给汽车打蜡,打蜡时需要独占整部车,一次打一部分蜡,等待抛光,然后再打一 ...
- ebtablesBridge
ebtables和iptables类似,都是Linux系统下网络数据包过滤的配置工具.既然称之为配置工具,就是说过滤功能是由内核底层提供支持的,这两个工具只是负责制定过滤的rules. ebtable ...
- VLC网页插件添加对火狐浏览器的支持
原文转自:http://blog.csdn.net/gsls200808/article/details/25536113 1.用<embed>标签 下面这段代码只支持火狐,不支持IE & ...
- android 反纠结app开发: 在线程中更新view
大体上想实现一个思路: 对一个view 的内容进行不停地变化, 通过按钮停止这种变化,以达到随机选择的目的. 开发过程中 使用textview 模拟, 建立线程 mythread = new Thr ...
- FZU-竞技游戏
Description John和Smith在玩一种竞技游戏.在游戏中,John给Smith由n个正整数组成的序列以及m条操作指令,需要Smith按照指令来对n个整数进行操作.其中每条指令都包括二个整 ...
- object- c 字符串操作
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- 【科研论文】W5100在远程电力质量监测设备中的应用
摘要: 针对传统电力质量监测方法实时性.多参数测试性能较差的缺点,提出了将以太网接入技术与电能采集相结合进行电力质量现场和远程在线监测的设计方案.硬件设计采用微控制器STM32FI03和以太网控制芯片 ...
- <微软的软件测试之道>读书笔记3
一.自动化的标准步骤: 1.环境初始化,并检查环境是否处于正确的状态,能否开始测试 2.执行步骤 3.判断结果,并将结果保存到其它地方以供检查分析 4.环境清理,清理本用例产生的垃圾(临时文件.环境变 ...
- [Redux] Avoiding Object Mutations with Object.assign() and ...spread
Learn how to use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects. ...
- Js 正则表达式知识测试
本文对javascript中正则表达式进行了总结汇总,将知识点和注意点都理了一下,并附上2个练习题,供大家参考学习. 正则表达式: 1.什么是RegExp?RegExp是正则表达式的缩写.RegExp ...