Masiar
Masiar

Reputation: 21352

nodejs shutdown hook on killall

I'm trying to bind some shutdown function to my nodejs application (version 0.8.12). Since I'm spawning A LOT of child processes and working in a distributed evironment, I'm currently killing the application through

var n = spawn('killall', ['node']);

The problem is that with this apparently the on('exit', ...) logic is no longer working, indeed I have something like this:

process.on('exit', function() {
    if(exit_cb)
        exit_cb()
    else
        console.log('About to exit.');
});  

And it's not firing whenever I kill the application.

Is there a way to add a shutdown hook with a killall command or should I find another way to kill my child processes in order to have the hook working?

Thanks

Upvotes: 2

Views: 1286

Answers (1)

Vadim Baryshev
Vadim Baryshev

Reputation: 26189

You need to listen for SIGTERM signal that killall sends by dafault. But also you need to stop your process manually after all jobs was finished.

process.on('SIGTERM', function() {
    if(exit_cb) {
        exit_cb();
        process.exit();
    } else {
        console.log('About to exit.');
        process.exit();
    }
});  

Upvotes: 3

Related Questions