Skip to content

Instantly share code, notes, and snippets.

@davidlares
Last active July 26, 2022 17:34
Show Gist options
  • Save davidlares/db10235625bb31ec367b0e8bd96eb8fe to your computer and use it in GitHub Desktop.
Save davidlares/db10235625bb31ec367b0e8bd96eb8fe to your computer and use it in GitHub Desktop.
NodeJS' child_process (fork & exec) functions PoC
const {fork} = require('child_process')
const path = require('path')
let cmd = fork(path.join(__dirname, 'run.js'), [], {cwd: process.cwd(), stdio: ['pipe', 'ipc']})
// sending
cmd.send('david')
// receiving
cmd.on('message', (data) => {
console.log(data) // Hello david
})
// exit (close)
cmd.on('close', (code) => {
console.log(code)
})
const {exec} = require('child_process')
process.on("message", (message) => {
exec(`sh script.sh ${message}`, (error, stdout, stderr) => {
if(err) {
console.error('exec wrong')
return
}
if(stderr) {
console.log('stderr wrong')
return
}
// transmitting the stdout
process.send(stdout)
})
})
echo "Hello $1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment