当前位置:网站首页>nodejs实现定时任务
nodejs实现定时任务
2022-07-22 18:05:00 【Fu_Tianshu】
本例使用cron来实现定时任务。时间格式支持cron格式和moment格式两种,本例使用moment格式。
依赖
| 依赖 | 作用 | 链接 |
|---|---|---|
| cron | 定时任务对象 | https://github.com/kelektiv/node-cron |
| moment | 设置时间格式 | https://momentjs.com/ |
| shelljs | 执行shell命令 | https://github.com/shelljs/shelljs |
| pm2 | 守护进程、退出进程 | https://pm2.keymetrics.io/docs/usage/quick-start/ |
本例使用pm2对进程进行守护,如下为ecosystem.config.js文件内容。
ecosystem.config.js
module.exports = {
apps : [
{
name: "timing-task",
script: "/home/projects/index.js"
}
]
};
index.js
const CronJob = require('cron').CronJob;
const moment = require("moment");
const shell = require("shelljs");
const pm2 = require('pm2');
let job = new CronJob({
cronTime: moment("2021-09-05 18:02:00", "YYYY-MM-DD HH:mm:ss").toDate(),
onTick: () => {
shell.exec("cp -r /home/projects/timing-task/dir1 /home/projects/timing-task/dir2");
console.log("运行结束," + moment().format("YYYY-MM-DD HH:mm:ss"));
pm2.stop("timing-task", () => {
});
},
onComplete: null,
timeZone: "Asia/Chongqing",
start: true
});
pm2启动index.js
pm2 start ecosystem.config.js --only timing-task
边栏推荐
- Jetpack -- mediatorlivedata source code analysis of livedata extension
- 合工大苍穹战队视觉组培训Day1——机器学习,学会使用YOLO模型
- jetson agx orin换源
- tensorflow——tf.train.slice_input_producer,tf.train.string_input_producer两种队列批量读取方式研究
- *编码理解*Pytorch中常见的函数解析
- Jetpack -- transformations of livedata extension
- *论文篇*EmotiCon: Context-Aware Multimodal Emotion Recognition using Frege’s Principle论文翻译
- *After inputting the picture size, the number of channels changes and the printing of network structure and parameter quantity, which is suitable for querying your own network parameter size
- 合工大苍穹战队视觉组培训Day4——传统视觉,轮廓识别
- opencv 项目-信用卡识别(学习记录)
猜你喜欢
随机推荐
Pytorch模型转Tensorflow模型的那些事
Using tensorflow to preprocess the input image to get the input layer of neural network
AHB协议相关
*实用篇*输入图片大小后,通道数变化和网络结构及参数量的打印,适用查询自己的网络参数大小
*Environment configuration *win10 test after installing CUDA, cudnn, pytorch GPU, tensorflow GPU
*Precision optimization * optimization strategy 1: network +sam optimizer
YOLO—V1细节分析
*Understand the basic functions in *cv2 (plus code practice)
LVGL:模拟器仿真
项目实战-文档扫描OCR识别
案例实战-全景图像拼接:特征匹配法
Espressif 玩转 PWM
Implementation of handwritten numeral code recognition (pytorch)
*After inputting the picture size, the number of channels changes and the printing of network structure and parameter quantity, which is suitable for querying your own network parameter size
*编码理解*Pytorch中常见的函数解析
GIC介绍 (三)——GIC400 Register
Jetpack - mediatorlivedata of livedata extension
持续学习、终身学习、情景记忆、记忆模块论文总结——梯度情景记忆促进持续学习
Jetpack -- transformations of livedata extension
线性模型之线性分类器之间的博弈









