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 ...
随机推荐
- 关于unsigned int和int的加法
补码(two's complement) 在计算机系统中,数值一律用补码来表示和存储.原因在于,使用补码,可以将符号位和数值域统一处理:同时,加法和减法也可以统一处理.此外,补码与原码相互转换,其运算 ...
- 黑马程序员_<<properties,打印流,合并流,分割流>>
--------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1. Properties 1.概述 Pro ...
- Git详解之二 Git基础
Git 基础 读完本章你就能上手使用 Git 了.本章将介绍几个最基本的,也是最常用的 Git 命令,以后绝大多数时间里用到的也就是这几个命令.读完本章,你就能初始化一个新的代码仓库,做一些适当配置: ...
- lesson4:使用锁Lock来解决重复下单的问题
demo源码:https://github.com/mantuliu/javaAdvance 中的类Lesson4IndependentLock 在上一节中,我们分析了Lock的源代码并一起实践了粗粒 ...
- Android Studio 2.1.x 关联SDK API Source
问题: 看图=>,当在android studio里ctrl+鼠标左键查看例如: TextUtils.isEmpty(content);这段代码的isEmpty方法的实现的时候经常就跑到如图所示 ...
- IOS学习教程
http://edu.51cto.com/course/course_id-566.html
- Meth | apt-get update ,upgarde 和dist-upgrade 的区别
#sudo apt-get update 获得最近的软件包的列表:列表中包含一些包的信息,比如这个包是否更新过#sudo apt-get dist-upgrade 如果这个包没有发布更新,就不管它:如 ...
- java基础01
1. /** * JDK: (Java Development ToolKit) java开发工具包.JDK是整个java的核心! * 包括了java运行环境 JRE(Java Runtime Env ...
- Dev GridControl 按条件合并相同单元格
Dev 默认的合并方式,只要(垂直方向)相邻两个单元格的值相同都会进行合并,这种方式并不是最优的,所以需要在进行合并的过程中进行判断. 方式如下: 1:先设置需要合并的列为允许合并 OptionsVi ...
- 设为首页 收藏(IE可用)
function SetHome(obj, vrl) { try { obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vr ...