Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
CodeWithSid675 opened this issue Feb 26, 2020 · 2 comments

Comments

@CodeWithSid675
Copy link

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);
});

@wahengchang
Copy link
Owner

there are two things that I can think about may help:

1- killing child process

const { spawn } = require('child_process');
const grep = spawn('grep', ['ssh']);
grep.on('close', (code, signal) => {
  console.log(
    `child process terminated due to receipt of signal ${signal}`);
});
// Send SIGHUP to process

2- wraping your child process as js (promise)

as I see your commandList are all js script, why dont u execute them by JS function ?

changing this to

var commandList = [
"node script1.js",
"node script2.js",
"node script3.js"
]
var commandList = [fun1, fun2, fun3]
shell.series(commandList , async function(err,res){
 await fun1()
 await fun2()
 await fun3()
});

@CodeWithSid675
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants