diff --git a/bin/pm2 b/bin/pm2 index bb066807..146d0ee2 100755 --- a/bin/pm2 +++ b/bin/pm2 @@ -506,7 +506,9 @@ commander.command('ilogs') commander.command('kill') .description('kill daemon') .action(failOnUnknown(function(arg) { - CLI.killDaemon(); + CLI.killDaemon(function() { + process.exit(cst.SUCCESS_EXIT); + }); })); // diff --git a/lib/God/ActionMethods.js b/lib/God/ActionMethods.js index e9c40631..cdd0dc1c 100644 --- a/lib/God/ActionMethods.js +++ b/lib/God/ActionMethods.js @@ -16,6 +16,7 @@ var pkg = require('../../package.json'); var pidusage = require('pidusage'); var util = require('util'); +var Satan = require('../Satan'); var debug = require('debug')('pm2:ActionMethod'); var Common = require('../Common'); @@ -389,17 +390,29 @@ module.exports = function(God) { * @param {} cb * @return */ - God.killMe = function(env, cb) { + God.killMe = function(opts, cb) { console.log('PM2 is being killed via kill method'); - cb(null, {msg : 'PM2 being stopped'}); + God.bus.emit('pm2:kill', { + status : 'killed', + msg : 'pm2 has been killed via CLI' + }); - setTimeout(function() { - God.bus.emit('pm2:kill', { - status : 'killed', - msg : 'pm2 has been killed via CLI' + /** + * Cleanly kill pm2 + */ + Satan.rpc_socket.close(function() { + console.log('RPC socket closed'); + Satan.pub_socket.close(function() { + console.log('PUB socket closed'); + + process.kill(parseInt(opts.pid), 'SIGUSR2'); + + setTimeout(function() { + process.exit(cst.SUCCESS_EXIT); + }, 2); }); - }, 50); + }); }; diff --git a/lib/Satan.js b/lib/Satan.js index ddd50ebe..f5967ca4 100644 --- a/lib/Satan.js +++ b/lib/Satan.js @@ -281,22 +281,6 @@ Satan.remoteWrapper = function() { return false; return pub.emit(this.event, JSON.parse(Stringify(data_v))); }); - - /** - * Cleanly kill pm2 - */ - God.bus.on('pm2:kill', function() { - console.log('killing PM2 via Satan'); - self.rpc_socket.close(function() { - console.log('RPC socket closed'); - self.pub_socket.close(function() { - console.log('PUB socket closed'); - console.log('exiting PM2'); - process.exit(cst.SUCCESS_EXIT); - }); - }); - }); - }; /** @@ -527,14 +511,16 @@ Satan.notifyGod = function(action_name, id, cb) { * @return */ Satan.killDaemon = function killDaemon(fn) { - Satan.executeRemote('killMe', {}, function() { + process.on('SIGUSR2', function() { + debug('Received SIGUSR2'); Satan.disconnectRPC(function() { - setTimeout(function() { - return fn ? fn(null, {success:true}) : false; - }, 200); - return false; + debug('RPC disconnected'); + return fn ? fn(null, {success:true}) : false; }); }); + + // Kill daemon + Satan.executeRemote('killMe', {pid : process.pid}, function() {}); }; /**