Created
March 27, 2021 00:06
-
-
Save gukandrew/9f7e9ff081c12840304895fc7745b304 to your computer and use it in GitHub Desktop.
Spawn child process in node, listen events and log exit code
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 spawn = require('child_process').spawn | |
const proc = spawn( | |
'pgrep', | |
[ | |
'-f', | |
'sh', | |
] | |
) | |
proc.stdout.on('close', (msg) => { | |
console.log('close', msg) | |
}) | |
proc.stdout.on('disconnect', (msg) => { | |
console.log('disconnect', msg) | |
}) | |
proc.stdout.on('error', (msg) => { | |
console.log('error', msg) | |
}) | |
proc.on('exit', (msg) => { | |
console.log('exit', msg) | |
}) | |
proc.stdout.on('exit', (msg) => { | |
console.log('exit', msg) | |
}) | |
proc.stdout.on('message', (msg) => { | |
console.log('message', msg) | |
}) | |
proc.stdout.on('data', (msg) => { | |
console.log('data', msg.toString()) | |
}) | |
proc.stdout.on('spawn', (msg) => { | |
console.log('spawn', msg) | |
}) | |
proc.stdout.on('end', (msg) => { | |
console.log('end', msg) | |
}) | |
proc.stderr.on('data', (msg) => { | |
console.log('edata', msg.toString()) | |
}) | |
// $ node ./test.js | |
// data 143 | |
// 1380 | |
// 1404 | |
// 1481 | |
// 1558 | |
// 1559 | |
// 1587 | |
// 1639 | |
// 1750 | |
// 1774 | |
// 1804 | |
// 1813 | |
// 1960 | |
// 3096 | |
// 3112 | |
// 3224 | |
// 3225 | |
// 3226 | |
// 5776 | |
// 72867 | |
// 78780 | |
// 83649 | |
// 92371 | |
// 96371 | |
// 96385 | |
// 96402 | |
// 96438 | |
// 96481 | |
// 96499 | |
// 99139 | |
// end undefined | |
// exit 0 | |
// close false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment