Skip to content

Instantly share code, notes, and snippets.

@dominhhai
Created January 31, 2016 02:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominhhai/aa7f3314ad27e2c50fd5 to your computer and use it in GitHub Desktop.
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')`.
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