简体   繁体   中英

close server on node.js

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM