I need to start the bash terminal, sequentially execute several commands, collect their results and quit.
What is the correct way to do this in nodejs?
I tried to achieve this with child_process.spawn but it doesn't work as I have expected even with a single command.
Here is the simplified code:
const process = spawn(`bash`, [])
// wait for the process to spawn
await new Promise<void>(resolve => process.once(`spawn`, resolve))
// log any output (expected to be the current node version)
process.stdout.on(`data`, data => console.log(data))
// wait for "node --version" to execute
await new Promise<void>(resolve => process.stdin.write(`node --version\n`, `utf8`, () => resolve()))
// wait for the process to end
await new Promise<void>(resolve => process.once(`close`, resolve))
The problem here is that I do not receive any outputs in stdout while await process.once('spawn') works fine.
I have logged stderr and every other event like process.on and stdout.on('error') but they are all empty. So I'm wondering what is the problem here.
In addition, google has tons of examples on how to run a single command. But I need to run several in the same terminal, wait between each call and collect individual results from stdout.
I'm not sure how to do this if this doesn't work as expected with the single command.
There are two things that may cause the problem. First is the last code executes resolve() right away allowing for the code execution to move to the next instructions immediately. Second is console.log(data) might not be enough to print the output. As I observed it, data is a Buffer, and not a string.
Please try this code and see if you get any useful message:
const { spawn } = require('child_process');
async function main() {
console.log("(console.log test)");
const process = spawn(`bash`, [])
// wait for the process to spawn
await new Promise(resolve => process.once(`spawn`, resolve))
// log any output (expected to be the current node version)
process.stdout.on(`data`, data => console.log(data.toString()))
// log any stderr
process.stderr.on(`data`, data => console.log(data.toString()))
// wait for "node --version" to execute
await new Promise(resolve => process.stdin.write(`exec node --version\n`, `utf8`, () => resolve()))
// wait for stdout and stderr stream to end, and process to close
await Promise.all([
new Promise(resolve => process.stdout.on('end', resolve)),
new Promise(resolve => process.stderr.on('end', resolve)),
new Promise(resolve => process.once(`close`, resolve))
])
}
main()
Use shelljs package and implement a helper function to run each command. Helper function:
const shell = require('shelljs');
async function runShellCmd(cmd) {
return new Promise((resolve, reject) => {
shell.exec(cmd, async (code, stdout, stderr) => {
if (!code) {
return resolve(stdout);
}
return reject(stderr);
});
});
}
// run your commands here
In case of running multiple command in one instance, I would go with a .sh script:
node file.js to do additional processing (e.g. calculation, execute other commands, async/await queries) and then export the values needed for the environment to continue either via file or environment variablesIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With