Created
January 31, 2016 02:42
-
-
Save dominhhai/aa7f3314ad27e2c50fd5 to your computer and use it in GitHub Desktop.
Stop Nodejs server with cli. Finding server log by `log.endsWith('./bin/www')`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const exec = require('child_process').exec | |
const SPACE = ' ' | |
exec('ps aux | grep node', (err, stdout, stderr) => { | |
if (err !== null) { | |
return console.log(`exec error: ${err}`) | |
} | |
var data = stdout.toString().split('\n') | |
data.forEach((log) => { | |
if (log.endsWith('./bin/www')) { | |
var pid = getSvrPid(log) | |
exec(`kill -9 ${pid}`, (err, stdout, stderr) => { | |
if (err !== null) { | |
return console.log(`exec error: ${err}`) | |
} | |
console.log(`killed process: ${stdout}`) | |
}) | |
} | |
}) | |
}) | |
function getSvrPid (log) { | |
var pid = -1 | |
for (var i = 0; i < log.length; i++) { | |
if (pid === -1) { // skip username | |
if (log[i] === SPACE) pid = 0 | |
} else if (pid === 0) { // skip space | |
if (log[i] !== SPACE) pid = log[i] | |
} else { | |
if (log[i] === SPACE) break | |
pid += log[i] | |
} | |
} | |
return pid | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment