Use child_process.execSync but keep output in console

后端 未结 4 1047
日久生厌
日久生厌 2020-12-07 10:43

I\'d like to use the execSync method which was added in NodeJS 0.12 but still have the output in the console window from which i ran the Node script.

E.

相关标签:
4条回答
  • 2020-12-07 11:04

    Unless you redirect stdout and stderr as the accepted answer suggests, this is not possible with execSync or spawnSync. Without redirecting stdout and stderr those commands only return stdout and stderr when the command is completed.

    To do this without redirecting stdout and stderr, you are going to need to use spawn to do this but it's pretty straight forward:

    var spawn = require('child_process').spawn;
    
    //kick off process of listing files
    var child = spawn('ls', ['-l', '/']);
    
    //spit stdout to screen
    child.stdout.on('data', function (data) {   process.stdout.write(data.toString());  });
    
    //spit stderr to screen
    child.stderr.on('data', function (data) {   process.stdout.write(data.toString());  });
    
    child.on('close', function (code) { 
        console.log("Finished with code " + code);
    });
    

    I used an ls command that recursively lists files so that you can test it quickly. Spawn takes as first argument the executable name you are trying to run and as it's second argument it takes an array of strings representing each parameter you want to pass to that executable.

    However, if you are set on using execSync and can't redirect stdout or stderr for some reason, you can open up another terminal like xterm and pass it a command like so:

    var execSync = require('child_process').execSync;
    
    execSync("xterm -title RecursiveFileListing -e ls -latkR /");
    

    This will allow you to see what your command is doing in the new terminal but still have the synchronous call.

    0 讨论(0)
  • 2020-12-07 11:10

    You can pass the parent´s stdio to the child process if that´s what you want:

    require('child_process').execSync(
        'rsync -avAXz --info=progress2 "/src" "/dest"',
        {stdio: 'inherit'}
    );
    
    0 讨论(0)
  • 2020-12-07 11:25

    You can simply use .toString().

    var result = require('child_process').execSync('rsync -avAXz --info=progress2 "/src" "/dest"').toString();
    console.log(result);
    

    This has been tested on Node v8.5.0, I'm not sure about previous versions. According to @etov, it doesn't work on v6.3.1 - I'm not sure about in-between.


    Edit: Looking back on this, I've realised that it doesn't actually answer the specific question because it doesn't show the output to you 'live' — only once the command has finished running.

    However, I'm leaving this answer here because I know quite a few people come across this question just looking for how to print the result of the command after execution.

    0 讨论(0)
  • 2020-12-07 11:29

    Simply:

     try {
        const cmd = 'git rev-parse --is-inside-work-tree';
        execSync(cmd).toString();
     } catch (error) {
        console.log(`Status Code: ${error.status} with '${error.message}'`;
     }
    

    Ref: https://stackoverflow.com/a/43077917/104085

    // nodejs
    var execSync = require('child_process').execSync;
    
    // typescript
    const { execSync } = require("child_process");
    
     try {
        const cmd = 'git rev-parse --is-inside-work-tree';
        execSync(cmd).toString();
     } catch (error) {
        error.status;  // 0 : successful exit, but here in exception it has to be greater than 0
        error.message; // Holds the message you typically want.
        error.stderr;  // Holds the stderr output. Use `.toString()`.
        error.stdout;  // Holds the stdout output. Use `.toString()`.
     }
    

    When command runs successful:

    0 讨论(0)
提交回复
热议问题