All Projects → wahengchang → nodejs-cron-job-must-know

wahengchang / nodejs-cron-job-must-know

Licence: other
it is an example of running node.js script with every certain period(cron job)

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to nodejs-cron-job-must-know

Crono
A time-based background job scheduler daemon (just like Cron) for Rails
Stars: ✭ 637 (+1720%)
Mutual labels:  cron, schedule, scheduler
Gocron
Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
Stars: ✭ 605 (+1628.57%)
Mutual labels:  cron, schedule, scheduler
scheduler
Task Scheduler for Laravel applications. UI from scratch
Stars: ✭ 18 (-48.57%)
Mutual labels:  cron, schedule, scheduler
croner
Trigger functions and/or evaluate cron expressions in JavaScript. No dependencies. Most features. All environments.
Stars: ✭ 169 (+382.86%)
Mutual labels:  cron, schedule, scheduler
ckron
🐋 A cron-like job scheduler for docker
Stars: ✭ 37 (+5.71%)
Mutual labels:  cron, scheduler, cronjob
Tiktok
Python web visualize build on the awesome web framework sanic
Stars: ✭ 55 (+57.14%)
Mutual labels:  cron, scheduler, cron-jobs
linda
Linda is a simple dispatcher library.
Stars: ✭ 12 (-65.71%)
Mutual labels:  cron, schedule, scheduler
Cronscheduler.aspnetcore
Cron Scheduler for AspNetCore 2.x/3.x or DotNetCore 2.x/3.x Self-hosted
Stars: ✭ 100 (+185.71%)
Mutual labels:  cron, scheduler, cron-jobs
EasyCronJob
This repository provides easy cron job to your application on IHostedService.
Stars: ✭ 66 (+88.57%)
Mutual labels:  cron, cron-jobs, cronjob
time.clj
time util for Clojure(Script)
Stars: ✭ 45 (+28.57%)
Mutual labels:  cron, scheduler, cronjob
gymbox-bot
Simplify the booking of a gymbox class.
Stars: ✭ 21 (-40%)
Mutual labels:  cron, schedule
rhythm
Time-based job scheduler for Apache Mesos
Stars: ✭ 30 (-14.29%)
Mutual labels:  cron, scheduler
watchman
📆 更夫(watchman)是一款可视化的定时任务配置 Web 工具,麻麻不用担心我漏掉任何更新啦!
Stars: ✭ 40 (+14.29%)
Mutual labels:  cron, schedule
angular-gantt-schedule-timeline-calendar-example
Angular gantt-schedule-timeline-calendar usage example
Stars: ✭ 15 (-57.14%)
Mutual labels:  schedule, scheduler
MR.AspNetCore.Jobs
A background processing library for Asp.Net Core.
Stars: ✭ 59 (+68.57%)
Mutual labels:  schedule, cron-jobs
sidecloq
Recurring / Periodic / Scheduled / Cron job extension for Sidekiq
Stars: ✭ 81 (+131.43%)
Mutual labels:  cron, schedule
coo
Schedule Twitter updates with easy
Stars: ✭ 44 (+25.71%)
Mutual labels:  schedule, scheduler
krolib
Magic library and DSL to handle complex schedules
Stars: ✭ 19 (-45.71%)
Mutual labels:  schedule, scheduler
gronx
Lightweight, fast and dependency-free Cron expression parser (due checker), task scheduler and/or daemon for Golang (tested on v1.13 and above) and standalone usage
Stars: ✭ 206 (+488.57%)
Mutual labels:  scheduler, cronjob
ld-scheduler
Schedule Launch Darkly flags on or off
Stars: ✭ 14 (-60%)
Mutual labels:  schedule, scheduler

nodejs-cron-job-must-know

it is an example of running Node.js script with every certain period(cron and non-cron job)

Why don't we use Linux crontab

  • We can provide the full path to node /usr/local/bin/node in your cron job like:
    • 30 6 1 * * /usr/local/bin/node /home/steve/example/script.js
  • Or making a script with the command, and then adding that to cron:
    #!/usr/bin/env sh 
    node /home/campaigns/reporting/UNIT_TESTS/testCron.js > /home/campaigns/reporting/UNIT_TESTS/cron.log
    
  • The problem with the two above methods is messing up the path. The command is in absolute path, but the Node.js script uses relative path to import/require other modules. It causes the error of file not found. So we need to execute the cron under the directory of Node.js script, which contains all the modules which will be used.

Install

This lib is used to keep the cron-job alive, which triggers the node script at a certain time.

$ npm install --save node-cron

Usage

Import node-cron and schedule a task:

Read more

var cron = require('node-cron');
 
cron.schedule('* * * * *', function(){
  console.log('running a task every minute');
});

Run Node.js script in cron

  • To run script : $ node script1.js
  • And script: $ npm run script -- PeterGood
  • child_help.js is an amazing Node.js script from mout, which helps to manage multiple linux commands.

Start a Daemon, and run

$ node cronNodeScript

Execute script every 1 min

//execute every 1 min
cron.schedule('*/1 * * * *', function(){
    ....
});

Execute $ node script1.js and npm run script -- PeterGood every 1 min

//execute every 1 min
cron.schedule('*/1 * * * *', function(){
    var shell = require('./child_helper');

    var commandList = [
        "node script1.js",
        "npm run script -- PeterGood"
    ]

    shell.series(commandList , function(err){
    //    console.log('executed many commands in a row'); 
        console.log('done')
    });
});

Reference:

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].