X Tutup
The Wayback Machine - https://web.archive.org/web/20201023140730/https://github.com/nodejs/node/issues/35680
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No stdout event fired on 'data' for child_process #35680

Open
binrusas85 opened this issue Oct 16, 2020 · 1 comment
Open

No stdout event fired on 'data' for child_process #35680

binrusas85 opened this issue Oct 16, 2020 · 1 comment

Comments

@binrusas85
Copy link

@binrusas85 binrusas85 commented Oct 16, 2020

I implemented a static method that takes two input parameters: (1) script/command (2) args to be passed to the given script/command. The issue is that I'm not able to get the output. Note I'm running on a mac machine if that makes sense.

Here is my code:

`import { spawn } from 'child_process';
import { StringBuilder } from 'typescript-string-operations';

export class Exec {
/**
* To execute a given command with its args in a separate process
* without creating a new shell
* @static
* @param {string} cmd
* @param {string[]} args
* @returns {Promise}
* @memberof Exec
*/
static exec(cmd:string, args:string[]):Promise {
return new Promise((resolve, reject) => {
try{
let out:StringBuilder = new StringBuilder();
let child = spawn(cmd, args);

            child.stdout.on('data', (data) => {
                out.Append(data.toString());
            });

            child.stderr.on('data', (data) => {
                out.Append(data.toString())
            });
            child.on('disconnect', (code) => {
                let msg:string = `${code} : Disconnected due to the parent process 
                manually calls the child.disconnect function` ;
                reject(msg); 
            })
            child.on('error', (code) => {
                let msg:string = `${code} : The process could not be spawned or killed` ;
                reject(msg) ;
            })
            child.on('close', (code) => {
                let msg:string = `${code} : The stdio streams of a child process get closed`
                Log.logger().info(msg);

                if(code !== 0){
                    reject(msg);
                }else{
                    resolve(out.ToString());
                }
            })
            child.on('exit', (code, signal) => {
                let msg:string = `Exited with code ${code} signal ${signal}` ;
                if(code !== 0){
                    reject(msg);
                }
            });
        } catch(err){
            console.log(`Failed due to error: ${err}`);
            reject(err.message);
        }
    });
}

}

// try to run the following line inside a function with 'async' word
let p = await Exec.exec('ps', ['ax']);

`

@mmomtchev
Copy link

@mmomtchev mmomtchev commented Oct 20, 2020

@binrusas85 After removing the Typescript part to try your code, the stdout event does get fired and your static function does return the output of ps ax both on OS X and Linux
Whatever your problem is, it is not linked to Node or OS X

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.
X Tutup