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.
CodePudding user response:
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()
CodePudding user response:
I'm not sure what the issue is, because I followed the docs, added a function returning a promise and it seems to work
let {spawn}=require('child_process') //spawn module
async function exec(command){
return await new Promise(resolve=>{
var toReturn="" //text to return
let options={env:process.env,cwd:undefined,shell:true}
let myChild=spawn(command,options)
myChild.stdout.on('data',txt=>toReturn =txt)
myChild.stderr.on('data',txt=>toReturn =txt)
myChild.on('close',()=>resolve(toReturn))
})
};
(async()=>{
console.log(await exec('ls\nrm --help')) //2 commands in one child process
//exec('bash some_file_full_of_bash_commands.txt') //also works
})()
