Last active
July 26, 2022 17:34
-
-
Save davidlares/db10235625bb31ec367b0e8bd96eb8fe to your computer and use it in GitHub Desktop.
NodeJS' child_process (fork & exec) functions PoC
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 {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) | |
}) |
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') | |
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) | |
}) | |
}) |
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
echo "Hello $1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment