diff --git a/cli/cmd.js b/cli/cmd.js index d04127443..5e96f8c7e 100644 --- a/cli/cmd.js +++ b/cli/cmd.js @@ -292,13 +292,28 @@ const setup = function setup() { }; var child = new(forever.Monitor)(path.join(__dirname, '..', 'app.js'), foreverOptions); if (options.action == "start") { - child.start(); - fs.appendFileSync(pidpath, process.pid+'|'); + try { + fs.appendFileSync(pidpath, process.pid+'|'); + child.start(); + } catch (piderr) { + console.error('failed to start microgateway: ' + piderr); + process.exit(1); + } } else { - var pids = fs.readFileSync(pidpath,'utf8').split('|'); - process.kill(pids[1], 'SIGINT'); - fs.unlinkSync(pidpath); - process.kill(pids[0], 'SIGINT'); + try { + var pids = fs.readFileSync(pidpath,'utf8').split('|'); + if (pids) { + pids.forEach(function(pid){ + process.kill(parseInt(pid), 'SIGINT'); + }); + fs.unlinkSync(pidpath); + } else { + console.log('pid file not found. please run this command from the folder where microgateway was started.') + } + } catch (piderr) { + console.error('failed to stop microgateway: ' + piderr); + process.exit(1); + } } }); diff --git a/cli/lib/gateway.js b/cli/lib/gateway.js index 42de6257c..0b0f77f64 100644 --- a/cli/lib/gateway.js +++ b/cli/lib/gateway.js @@ -10,6 +10,7 @@ const JsonSocket = require('./json-socket'); const configLocations = require('../../config/locations'); const isWin = /^win/.test(process.platform); const ipcPath = configLocations.getIPCFilePath(); +const pidPath = configLocations.getPIDFilePath(); const defaultPollInterval = 600; const uuid = require('uuid/v1'); const debug = require('debug')('microgateway'); @@ -118,12 +119,13 @@ Gateway.prototype.start = (options) => { mgCluster.run(); console.log('PROCESS PID : ' + process.pid); - fs.appendFileSync(configLocations.getPIDFilePath(), process.pid); + fs.appendFileSync(pidPath, process.pid); process.on('exit', () => { if (!isWin) { console.log('Removing the socket file as part of cleanup'); fs.unlinkSync(ipcPath); + fs.unlinkSync(pidPath) } });