0

I use node.js to load dynamic crontab to to operation system and I want to add close function that when I close the node.js server it call this function and remove all the crontabs

Code:

var server = app.listen(8080, function() {
  loadCronTab("*/1 * * * * date");
  console.log('Server running...');
});


server.on('close', function () {
  closeCronTab();
  console.log("Closed");
  redis.quit();
});

The remove function is:

function closeCronTab()
{
    require('crontab').load(function(err, crontab) {
      if (err) {
        return console.error(err);
      }
      var command = 'ls -l';
      crontab.remove({command:command});
      crontab.save(function(err, crontab) {

      });
    });
}

I have use var express with my node.js it can help?

I don't know how to write the close function syntax.. =(

I try

var server = app.listen(8080, function() {
  console.log('Server running...');
});

server.close(function(){
  closeCronTab();
  console.log("Closed");
});

But it runs the server and closes it after a second..

Muath
  • 4,351
  • 12
  • 42
  • 69
royb
  • 693
  • 3
  • 9
  • 20

1 Answers1

0

Check this answer about process exiting events, but note that it's not guaranteed. Power loss, SIGKILL (kill -9), segfault, and other hard exits can't be handled.

Community
  • 1
  • 1
furydevoid
  • 1,391
  • 8
  • 7