Giter Site home page Giter Site logo

wahengchang / nodejs-cron-job-must-know Goto Github PK

View Code? Open in Web Editor NEW
38.0 6.0 15.0 7 KB

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

JavaScript 100.00%
nodejs javascript cron cron-jobs cronjob reactjs scheduler schedule example tutorial

nodejs-cron-job-must-know's Introduction

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:

nodejs-cron-job-must-know's People

Contributors

bryanvoss avatar wahengchang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nodejs-cron-job-must-know's Issues

Misspelling

//exceute every 2:30am morning
// cron.schedule('30 2 * * *', function(){
// console.log('running a task every two minutes');
// });

//exceute every 1 min

exceute --> execute

Need help in skipping cronjob when already scripts for previous cronjob are running

I have written a cron job as given below

var cron = require('node-cron');
var taskMap = {};
let job = cron.schedule('*/1 * * * * ', ()=>{
var shell = require('./utils/child_helper');

var commandList = [
"node *****",
"node *****",
"node ******"
]

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

which need to run on every 1 min but the scripts inside the cron may take more than 1 min.
I need to skip the cron for the moment when the scripts are still running .
Can anyone help me on it.

Calling cron from app.js as below code

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
var port = 3000;
require('./cronController').job.start();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.listen(port , ()=>{
let dateTime = new Date();
let message = 'Server is running on Port :- ' + port + ' started at :- ' + dateTime;
console.log(message);
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.