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的更多相关文章

  1. 基于agenda的Nodejs定时任务管理框架搭建

    0.背景 在大型项目中,定时任务的应用场景越来越广.一般来说,按照微服务的思想,我们会将定时任务单独部署一套服务,核心的业务接口独立到另一个服务中,从而降低相互之间的耦合程度.在需要使用定时任务时,只 ...

  2. NodeJS定时任务

    在实际开发项目中,会遇到很多定时任务的工作.比如:定时导出某些数据.定时发送消息或邮件给用户.定时备份什么类型的文件等等 一般可以写个定时器,来完成相应的需求,在node.js中自已实现也非常容易,接 ...

  3. NodeJS(node.exe, npm, express, live-server)安装

    1.下载node.exe 下载https://nodejs.org/en/download/current/ 创建D:\GreenSoftware\NodeJS目录,并将node.exe放到目录中. ...

  4. [Nodejs] 用node写个爬虫

    寻找爬取的目标 首先我们需要一个坚定的目标,于是找个一个比较好看一些网站,将一些信息统计一下,比如 url/tag/title/number...等信息 init(1, 2); //设置页数,现在是1 ...

  5. 【NodeJS】Node.JS 开发环境安装

    1.前言 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O ...

  6. Nodejs:Node.js模块机制小结

    今天读了<深入浅出Nodejs>的第二章:模块机制.现在做一个简单的小结. 序:模块机制大致从这几个部分来讲:JS模块机制的由来.CommonJS AMD CMD.Node模块机制和包和n ...

  7. Nodejs 定时任务

    安装扩展:node-schedule npm install node-schedule 1.linux Crontab风格 var schedule = require('node-schedule ...

  8. SpringBoot系列:Spring Boot定时任务Spring Schedule

    Spring Schedule是Spring提供的定时任务框架,相较于Quartz,Schedule更加简单易用,在中小型应用中,对于大部分需求,Schedule都可以胜任. 一.Spring Sch ...

  9. 定时任务模块 schedule

    # coding:utf-8 from learning_python.Telegram_push.check_hardware import check_cpu import schedule im ...

随机推荐

  1. HDU_1846——最简单的巴什博弈

    Problem Description 十年前读大学的时候,中国每年都要从国外引进一些电影大片,其中有一部电影就叫<勇敢者的游戏>(英文名称:Zathura),一直到现在,我依然对于电影中 ...

  2. Struts2初学习记录

    以下笔记内容来自尚硅谷_Struts2_佟刚老师的视频教程+自己一点点整理 来源免责声明 一. 1. VS 自实现: 1). 搭建 Struts2 的开发环境 2). 不需要显式的定义 Filter, ...

  3. (Relax 水题1.2)POJ 1032 Parliament(将n分解成若干个互不相等的整数的和,并且是这些整数的乘积最大)

    题意:给出一个数n,将其拆分为若干个互不相等的数字的和,要求这些数字的乘积最大. 分析:我们可以发现任何一个数字,只要能拆分成两个大于1的数字之和,那么这两个数字的乘积一定大于等于原数.也就是说,对于 ...

  4. iOS 设备和外部配件的通讯

    首先,如果我们的应用程序想跟外设传输数据,先要透过iphone的操作系统,也就是iphoneOS,而最开始的认证过程也是在外设和iphoneOS之间发生的,苹果为这个过程提供了一颗认证芯片(这颗芯片的 ...

  5. python network programming tutorial

    关于网络编程以及socket 等一些概念和函数介绍就不再重复了,这里示例性用python 编写客户端和服务器端. 一.最简单的客户端流程: 1. Create a socket 2. Connect ...

  6. [Angular 2] Passing Observables into Components with Async Pipe

    The components inside of your container components can easily accept Observables. You simply define ...

  7. WIN32读写INI文件方法

      在程序中经常要用到设置或者其他少量数据的存盘,以便程序在下一次执行的时候可以使用,比如说保存本次程序执行时窗口的位置.大小.一些用户设置的 数据等等,在 Dos 下编程的时候,我们一般自己产生一个 ...

  8. Embedded tomcat 7 servlet 3.0 annotations not working--转

    Question: I have a stripped down test project which contains a Servlet version 3.0, declared with an ...

  9. JuiceSSh破解分析

    JuiceSSH是一款免费的远程ssh客户端,感觉是一款挺优秀的软件,里边有一些高级功能需要购买高级版才能使用,这里便对其对高级功能的破解进行分析. 本文仅用于学习交流使用,请尊重作者,勿在网上肆意发 ...

  10. 《UNIX网络编程》之点对点通信

    思路: 点对点通信,其原理也比较简单,在前面回显服务器的基础上,我们分别在服务端和客户端都使用两个线程,一个线程负责发送数据包,一个线程负责接收数据包. 代码如下: 客户端: /*********** ...