Use child_process.execSync but keep output in console
Asked Answered
W

5

286

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.g., if I run a NodeJS script which has the following line I'd like to see the full output of the rsync command "live" inside the console:

require('child_process').execSync('rsync -avAXz --info=progress2 "/src" "/dest"');

I understand that execSync returns the output of the command and that I could print that to the console after execution, but this way I don't have "live" output.

Willie answered 8/5, 2015 at 23:23 Comment(0)
G
556

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'}
);
Goatsbeard answered 28/6, 2015 at 21:23 Comment(7)
What does stdio[0,1,2] mean?Microscopic
It means that the child process will use the parent's stdin, stdout and stderr streams. So when the child process writes to either of them, it will actually be written directly to the parent´s stream.Goatsbeard
This is a very valuable answer, as the official documentation is not really explicit about the expected syntax.Stoneblind
Instead of [0,1,2] I've used 'inherit', which is equivalent to [process.stdin, process.stdout, process.stderr] or [0,1,2] as per docsCavesson
@Booligoosh How is that simpler ??Kanishakanji
@Booligoosh Instead of simply adding {stdio:'inherit'}, you have to add .toString() and then call console.log manually with the result. In addition, it doesn't even fulfill the questions requirement of seeing the command output "live". I don't think it's "much simpler", in fact I don't think it's simpler at all.Kanishakanji
Is there any convenient way to both use inherit and return the output as a string?Placatory
T
55

You can simply use .toString().

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

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.

Tocsin answered 23/6, 2017 at 8:42 Comment(4)
This doesn't work on failure (status code != 0) because .execSync() throws an Error instance.Palliate
Doesn't work for me, i.e. output is only written after command finishes. Does this apply to a specific version? my node -v: v6.3.1Inculpable
Please consider updating the answer to note it's applicable only to certain node versions - this would make it more useful to othersInculpable
Downvote since ti's in now way relating to the question regarding output during the command is executed.Omeara
H
25

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.

Hoi answered 11/5, 2015 at 13:21 Comment(1)
The example using spawn may be correct, but the opening statement about not being about to use execSync is not accurate. See answer from @GoatsbeardJudijudicable
R
9

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://mcmap.net/q/109920/-node-js-accessing-the-exit-code-and-stderr-of-a-system-command

// 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()`.
 }

enter image description here

enter image description here

When command runs successful: enter image description here

Radical answered 22/11, 2020 at 14:56 Comment(1)
The pictures have console.log( but the code text just calls .toString(); and throws away the string result. This would only work if run in the interactive console, not in a script.Raman
C
4

Add {"encoding": "utf8"} in options.

execSync(`pwd`, {
  encoding: "utf8"
})
Constitutional answered 12/3, 2022 at 7:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.