Exec : display stdout “live”
I have this simple script :
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee', function(error, stdout, stderr) {
console.log(stdout);
});
where I simply execute a command to compile a coffee-script file. But stdout never get displayed in the console, because the command never ends (because of the -w option of coffee).
If I execute the command directly from the console I get message like this :
18:05:59 - compiled my_file.coffee
My question is : is it possible to display these messages with the node.js exec ? If yes how ? !
Thanks
node.js coffeescript
add a comment |
I have this simple script :
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee', function(error, stdout, stderr) {
console.log(stdout);
});
where I simply execute a command to compile a coffee-script file. But stdout never get displayed in the console, because the command never ends (because of the -w option of coffee).
If I execute the command directly from the console I get message like this :
18:05:59 - compiled my_file.coffee
My question is : is it possible to display these messages with the node.js exec ? If yes how ? !
Thanks
node.js coffeescript
I came here looking for capturing stdout from Python executable. Note that all of the below will work, but you need to run python with a "-u" option, to make outout unbuffered and thereby have live updates.
– Andy
Nov 5 '17 at 18:36
add a comment |
I have this simple script :
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee', function(error, stdout, stderr) {
console.log(stdout);
});
where I simply execute a command to compile a coffee-script file. But stdout never get displayed in the console, because the command never ends (because of the -w option of coffee).
If I execute the command directly from the console I get message like this :
18:05:59 - compiled my_file.coffee
My question is : is it possible to display these messages with the node.js exec ? If yes how ? !
Thanks
node.js coffeescript
I have this simple script :
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee', function(error, stdout, stderr) {
console.log(stdout);
});
where I simply execute a command to compile a coffee-script file. But stdout never get displayed in the console, because the command never ends (because of the -w option of coffee).
If I execute the command directly from the console I get message like this :
18:05:59 - compiled my_file.coffee
My question is : is it possible to display these messages with the node.js exec ? If yes how ? !
Thanks
node.js coffeescript
node.js coffeescript
edited Jun 5 '13 at 12:58
TrexXx
asked Apr 19 '12 at 16:07
TrexXxTrexXx
2,21021626
2,21021626
I came here looking for capturing stdout from Python executable. Note that all of the below will work, but you need to run python with a "-u" option, to make outout unbuffered and thereby have live updates.
– Andy
Nov 5 '17 at 18:36
add a comment |
I came here looking for capturing stdout from Python executable. Note that all of the below will work, but you need to run python with a "-u" option, to make outout unbuffered and thereby have live updates.
– Andy
Nov 5 '17 at 18:36
I came here looking for capturing stdout from Python executable. Note that all of the below will work, but you need to run python with a "-u" option, to make outout unbuffered and thereby have live updates.
– Andy
Nov 5 '17 at 18:36
I came here looking for capturing stdout from Python executable. Note that all of the below will work, but you need to run python with a "-u" option, to make outout unbuffered and thereby have live updates.
– Andy
Nov 5 '17 at 18:36
add a comment |
8 Answers
8
active
oldest
votes
Don't use exec. Use spawn which is an EventEmmiter object. Then you can listen to stdout/stderr events (spawn.stdout.on('data',callback..)) as they happen.
From NodeJS documentation:
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code.toString());
});
exec buffers the output and usually returns it when the command has finished executing.
17
Very nice. FYI: The stdout/stderr events callback argument 'data' is a buffer so call it with .toString()
– SergeL
May 9 '14 at 14:01
3
For those of you who can't get spawn to work on Windows, have a look at this great answer.
– tomekwi
Aug 27 '14 at 8:33
12
exec is also an EventEmitter at least in latest.
– Nikolay Tsenkov
Jun 27 '15 at 3:50
2
Also keep in mind that the callback will not be called, whenever the program outputs a newline. If you want to receive "events" from the child process, this process must flush the buffer (flush(stdout);in C) in order to fire events in Node.js.
– Julian F. Weinert
Mar 20 '16 at 1:18
5
+1 on exec also being an EventEmitter.. spent 2 hours on refactoring my string into an args array (very long and complicated ffmpeg command line).. only to find out I didn't really need to.
– deadconversations
Apr 4 '16 at 13:34
|
show 6 more comments
exec will also return a ChildProcess object that is an EventEmitter.
var exec = require('child_process').exec;
var coffeeProcess = exec('coffee -cw my_file.coffee');
coffeeProcess.stdout.on('data', function(data) {
console.log(data);
});
OR pipe the child process's stdout to the main stdout.
coffeeProcess.stdout.pipe(process.stdout);
OR inherit stdio using spawn
spawn('coffee -cw my_file.coffee', { stdio: 'inherit' });
Thanks, that worked for me.
– Eric
May 24 '15 at 14:56
29
Looks like this can be simplified by just usingpipe:coffeeProcess.stdout.pipe(process.stdout);
– Eric Freese
Aug 19 '15 at 15:00
3
@EricFreese's comment is what I was looking for, because I wanted to leverage stdout's characters replacement feature (harnessing protractor in a node script)
– LoremIpsum
Oct 31 '16 at 11:00
10
Simpler:spawn(cmd, argv, { stdio: 'inherit' }). See nodejs.org/api/child_process.html#child_process_options_stdio for different examples.
– Morgan Touverey Quilling
Mar 30 '17 at 14:39
2
+1 for @MorganTouvereyQuilling's suggestion to usespawnwithstdio: 'inherit'. It produces more accurate output thanexecand pipingstdout/stderr, for example when displaying the progress information from agit clone.
– Livven
Apr 18 '17 at 15:56
|
show 2 more comments
There are already several answers however none of them mention the best (and easiest) way to do this, which is using spawn and the { stdio: 'inherit' } option. It seems to produce the most accurate output, for example when displaying the progress information from a git clone.
Simply do this:
var spawn = require('child_process').spawn;
spawn('coffee', ['-cw', 'my_file.coffee'], { stdio: 'inherit' });
Credit to @MorganTouvereyQuilling for pointing this out in this comment.
I found that when the subprocess uses formatted output like colored text,stdio: "inherit"preserves that formatting whilechild.stdout.pipe(process.stdout)does not.
– Rikki Gibson
Sep 21 '17 at 19:59
This perfectly preserves output even on processes with complex output like the progress bars on npm installs. Awesome!
– Dave Koo
Jun 10 '18 at 18:17
add a comment |
I'd just like to add that one small issue with outputting the buffer strings from a spawned process with console.log() is that it adds newlines, which can spread your spawned process output over additional lines. If you output stdout or stderr with process.stdout.write() instead of console.log(), then you'll get the console output from the spawned process 'as is'.
I saw that solution here:
Node.js: printing to console without a trailing newline?
Hope that helps someone using the solution above (which is a great one for live output, even if it is from the documentation).
1
For even more accurate output usespawn(command, args, { stdio: 'inherit' }), as suggested by @MorganTouvereyQuilling here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 15:59
add a comment |
Inspired by Nathanael Smith's answer and Eric Freese's comment, it could be as simple as:
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee').stdout.pipe(process.stdout);
This seems to work fine for simple commands likelsbut fails for more complex commands such asnpm install. I even tried piping both stdout and stderr to their respective process objects.
– linuxdan
Aug 19 '16 at 15:15
@linuxdan it may be because npm is writing in stderr (i saw some write the progress bar there). you can pipe also stderr, or extend Tongfa solution to listen on stderr.
– Sergiu
Jan 16 '17 at 17:10
@linuxdan From what I've seen the most reliable way isspawn(command, args, { stdio: 'inherit' }), as suggested here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 16:03
add a comment |
After reviewing all the other answers, I ended up with this:
function oldSchoolMakeBuild(cb) {
var makeProcess = exec('make -C ./oldSchoolMakeBuild',
function (error, stdout, stderr) {
stderr && console.error(stderr);
cb(error);
});
makeProcess.stdout.on('data', function(data) {
process.stdout.write('oldSchoolMakeBuild: '+ data);
});
}
Sometimes data will be multiple lines, so the oldSchoolMakeBuild header will appear once for multiple lines. But this didn't bother me enough to change it.
add a comment |
I have found it helpful to add a custom exec script to my utilities that does this.
utilities.js
const { exec } = require('child_process')
module.exports.exec = (command) => {
const process = exec(command)
process.stdout.on('data', (data) => {
console.log('stdout: ' + data.toString())
})
process.stderr.on('data', (data) => {
console.log('stderr: ' + data.toString())
})
process.on('exit', (code) => {
console.log('child process exited with code ' + code.toString())
})
}
app.js
const { exec } = require('./utilities.js)
exec('coffee -cw my_file.coffee')
add a comment |
child_process.spawn returns an object with stdout and stderr streams.
You can tap on the stdout stream to read data that the child process sends back to Node. stdout being a stream has the "data", "end", and other events that streams have. spawn is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc.
so you can solve your problem using child_process.spawn as used below.
var spawn = require('child_process').spawn,
ls = spawn('coffee -cw my_file.coffee');
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('code ' + code.toString());
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10232192%2fexec-display-stdout-live%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't use exec. Use spawn which is an EventEmmiter object. Then you can listen to stdout/stderr events (spawn.stdout.on('data',callback..)) as they happen.
From NodeJS documentation:
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code.toString());
});
exec buffers the output and usually returns it when the command has finished executing.
17
Very nice. FYI: The stdout/stderr events callback argument 'data' is a buffer so call it with .toString()
– SergeL
May 9 '14 at 14:01
3
For those of you who can't get spawn to work on Windows, have a look at this great answer.
– tomekwi
Aug 27 '14 at 8:33
12
exec is also an EventEmitter at least in latest.
– Nikolay Tsenkov
Jun 27 '15 at 3:50
2
Also keep in mind that the callback will not be called, whenever the program outputs a newline. If you want to receive "events" from the child process, this process must flush the buffer (flush(stdout);in C) in order to fire events in Node.js.
– Julian F. Weinert
Mar 20 '16 at 1:18
5
+1 on exec also being an EventEmitter.. spent 2 hours on refactoring my string into an args array (very long and complicated ffmpeg command line).. only to find out I didn't really need to.
– deadconversations
Apr 4 '16 at 13:34
|
show 6 more comments
Don't use exec. Use spawn which is an EventEmmiter object. Then you can listen to stdout/stderr events (spawn.stdout.on('data',callback..)) as they happen.
From NodeJS documentation:
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code.toString());
});
exec buffers the output and usually returns it when the command has finished executing.
17
Very nice. FYI: The stdout/stderr events callback argument 'data' is a buffer so call it with .toString()
– SergeL
May 9 '14 at 14:01
3
For those of you who can't get spawn to work on Windows, have a look at this great answer.
– tomekwi
Aug 27 '14 at 8:33
12
exec is also an EventEmitter at least in latest.
– Nikolay Tsenkov
Jun 27 '15 at 3:50
2
Also keep in mind that the callback will not be called, whenever the program outputs a newline. If you want to receive "events" from the child process, this process must flush the buffer (flush(stdout);in C) in order to fire events in Node.js.
– Julian F. Weinert
Mar 20 '16 at 1:18
5
+1 on exec also being an EventEmitter.. spent 2 hours on refactoring my string into an args array (very long and complicated ffmpeg command line).. only to find out I didn't really need to.
– deadconversations
Apr 4 '16 at 13:34
|
show 6 more comments
Don't use exec. Use spawn which is an EventEmmiter object. Then you can listen to stdout/stderr events (spawn.stdout.on('data',callback..)) as they happen.
From NodeJS documentation:
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code.toString());
});
exec buffers the output and usually returns it when the command has finished executing.
Don't use exec. Use spawn which is an EventEmmiter object. Then you can listen to stdout/stderr events (spawn.stdout.on('data',callback..)) as they happen.
From NodeJS documentation:
var spawn = require('child_process').spawn,
ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code.toString());
});
exec buffers the output and usually returns it when the command has finished executing.
edited Jan 12 '17 at 14:59
Samuel Bolduc
9,50042349
9,50042349
answered Apr 19 '12 at 16:15
Pooria AzimiPooria Azimi
7,25652541
7,25652541
17
Very nice. FYI: The stdout/stderr events callback argument 'data' is a buffer so call it with .toString()
– SergeL
May 9 '14 at 14:01
3
For those of you who can't get spawn to work on Windows, have a look at this great answer.
– tomekwi
Aug 27 '14 at 8:33
12
exec is also an EventEmitter at least in latest.
– Nikolay Tsenkov
Jun 27 '15 at 3:50
2
Also keep in mind that the callback will not be called, whenever the program outputs a newline. If you want to receive "events" from the child process, this process must flush the buffer (flush(stdout);in C) in order to fire events in Node.js.
– Julian F. Weinert
Mar 20 '16 at 1:18
5
+1 on exec also being an EventEmitter.. spent 2 hours on refactoring my string into an args array (very long and complicated ffmpeg command line).. only to find out I didn't really need to.
– deadconversations
Apr 4 '16 at 13:34
|
show 6 more comments
17
Very nice. FYI: The stdout/stderr events callback argument 'data' is a buffer so call it with .toString()
– SergeL
May 9 '14 at 14:01
3
For those of you who can't get spawn to work on Windows, have a look at this great answer.
– tomekwi
Aug 27 '14 at 8:33
12
exec is also an EventEmitter at least in latest.
– Nikolay Tsenkov
Jun 27 '15 at 3:50
2
Also keep in mind that the callback will not be called, whenever the program outputs a newline. If you want to receive "events" from the child process, this process must flush the buffer (flush(stdout);in C) in order to fire events in Node.js.
– Julian F. Weinert
Mar 20 '16 at 1:18
5
+1 on exec also being an EventEmitter.. spent 2 hours on refactoring my string into an args array (very long and complicated ffmpeg command line).. only to find out I didn't really need to.
– deadconversations
Apr 4 '16 at 13:34
17
17
Very nice. FYI: The stdout/stderr events callback argument 'data' is a buffer so call it with .toString()
– SergeL
May 9 '14 at 14:01
Very nice. FYI: The stdout/stderr events callback argument 'data' is a buffer so call it with .toString()
– SergeL
May 9 '14 at 14:01
3
3
For those of you who can't get spawn to work on Windows, have a look at this great answer.
– tomekwi
Aug 27 '14 at 8:33
For those of you who can't get spawn to work on Windows, have a look at this great answer.
– tomekwi
Aug 27 '14 at 8:33
12
12
exec is also an EventEmitter at least in latest.
– Nikolay Tsenkov
Jun 27 '15 at 3:50
exec is also an EventEmitter at least in latest.
– Nikolay Tsenkov
Jun 27 '15 at 3:50
2
2
Also keep in mind that the callback will not be called, whenever the program outputs a newline. If you want to receive "events" from the child process, this process must flush the buffer (
flush(stdout); in C) in order to fire events in Node.js.– Julian F. Weinert
Mar 20 '16 at 1:18
Also keep in mind that the callback will not be called, whenever the program outputs a newline. If you want to receive "events" from the child process, this process must flush the buffer (
flush(stdout); in C) in order to fire events in Node.js.– Julian F. Weinert
Mar 20 '16 at 1:18
5
5
+1 on exec also being an EventEmitter.. spent 2 hours on refactoring my string into an args array (very long and complicated ffmpeg command line).. only to find out I didn't really need to.
– deadconversations
Apr 4 '16 at 13:34
+1 on exec also being an EventEmitter.. spent 2 hours on refactoring my string into an args array (very long and complicated ffmpeg command line).. only to find out I didn't really need to.
– deadconversations
Apr 4 '16 at 13:34
|
show 6 more comments
exec will also return a ChildProcess object that is an EventEmitter.
var exec = require('child_process').exec;
var coffeeProcess = exec('coffee -cw my_file.coffee');
coffeeProcess.stdout.on('data', function(data) {
console.log(data);
});
OR pipe the child process's stdout to the main stdout.
coffeeProcess.stdout.pipe(process.stdout);
OR inherit stdio using spawn
spawn('coffee -cw my_file.coffee', { stdio: 'inherit' });
Thanks, that worked for me.
– Eric
May 24 '15 at 14:56
29
Looks like this can be simplified by just usingpipe:coffeeProcess.stdout.pipe(process.stdout);
– Eric Freese
Aug 19 '15 at 15:00
3
@EricFreese's comment is what I was looking for, because I wanted to leverage stdout's characters replacement feature (harnessing protractor in a node script)
– LoremIpsum
Oct 31 '16 at 11:00
10
Simpler:spawn(cmd, argv, { stdio: 'inherit' }). See nodejs.org/api/child_process.html#child_process_options_stdio for different examples.
– Morgan Touverey Quilling
Mar 30 '17 at 14:39
2
+1 for @MorganTouvereyQuilling's suggestion to usespawnwithstdio: 'inherit'. It produces more accurate output thanexecand pipingstdout/stderr, for example when displaying the progress information from agit clone.
– Livven
Apr 18 '17 at 15:56
|
show 2 more comments
exec will also return a ChildProcess object that is an EventEmitter.
var exec = require('child_process').exec;
var coffeeProcess = exec('coffee -cw my_file.coffee');
coffeeProcess.stdout.on('data', function(data) {
console.log(data);
});
OR pipe the child process's stdout to the main stdout.
coffeeProcess.stdout.pipe(process.stdout);
OR inherit stdio using spawn
spawn('coffee -cw my_file.coffee', { stdio: 'inherit' });
Thanks, that worked for me.
– Eric
May 24 '15 at 14:56
29
Looks like this can be simplified by just usingpipe:coffeeProcess.stdout.pipe(process.stdout);
– Eric Freese
Aug 19 '15 at 15:00
3
@EricFreese's comment is what I was looking for, because I wanted to leverage stdout's characters replacement feature (harnessing protractor in a node script)
– LoremIpsum
Oct 31 '16 at 11:00
10
Simpler:spawn(cmd, argv, { stdio: 'inherit' }). See nodejs.org/api/child_process.html#child_process_options_stdio for different examples.
– Morgan Touverey Quilling
Mar 30 '17 at 14:39
2
+1 for @MorganTouvereyQuilling's suggestion to usespawnwithstdio: 'inherit'. It produces more accurate output thanexecand pipingstdout/stderr, for example when displaying the progress information from agit clone.
– Livven
Apr 18 '17 at 15:56
|
show 2 more comments
exec will also return a ChildProcess object that is an EventEmitter.
var exec = require('child_process').exec;
var coffeeProcess = exec('coffee -cw my_file.coffee');
coffeeProcess.stdout.on('data', function(data) {
console.log(data);
});
OR pipe the child process's stdout to the main stdout.
coffeeProcess.stdout.pipe(process.stdout);
OR inherit stdio using spawn
spawn('coffee -cw my_file.coffee', { stdio: 'inherit' });
exec will also return a ChildProcess object that is an EventEmitter.
var exec = require('child_process').exec;
var coffeeProcess = exec('coffee -cw my_file.coffee');
coffeeProcess.stdout.on('data', function(data) {
console.log(data);
});
OR pipe the child process's stdout to the main stdout.
coffeeProcess.stdout.pipe(process.stdout);
OR inherit stdio using spawn
spawn('coffee -cw my_file.coffee', { stdio: 'inherit' });
edited Jan 15 at 17:09
answered May 6 '15 at 18:35
Nathanael SmithNathanael Smith
1,6151910
1,6151910
Thanks, that worked for me.
– Eric
May 24 '15 at 14:56
29
Looks like this can be simplified by just usingpipe:coffeeProcess.stdout.pipe(process.stdout);
– Eric Freese
Aug 19 '15 at 15:00
3
@EricFreese's comment is what I was looking for, because I wanted to leverage stdout's characters replacement feature (harnessing protractor in a node script)
– LoremIpsum
Oct 31 '16 at 11:00
10
Simpler:spawn(cmd, argv, { stdio: 'inherit' }). See nodejs.org/api/child_process.html#child_process_options_stdio for different examples.
– Morgan Touverey Quilling
Mar 30 '17 at 14:39
2
+1 for @MorganTouvereyQuilling's suggestion to usespawnwithstdio: 'inherit'. It produces more accurate output thanexecand pipingstdout/stderr, for example when displaying the progress information from agit clone.
– Livven
Apr 18 '17 at 15:56
|
show 2 more comments
Thanks, that worked for me.
– Eric
May 24 '15 at 14:56
29
Looks like this can be simplified by just usingpipe:coffeeProcess.stdout.pipe(process.stdout);
– Eric Freese
Aug 19 '15 at 15:00
3
@EricFreese's comment is what I was looking for, because I wanted to leverage stdout's characters replacement feature (harnessing protractor in a node script)
– LoremIpsum
Oct 31 '16 at 11:00
10
Simpler:spawn(cmd, argv, { stdio: 'inherit' }). See nodejs.org/api/child_process.html#child_process_options_stdio for different examples.
– Morgan Touverey Quilling
Mar 30 '17 at 14:39
2
+1 for @MorganTouvereyQuilling's suggestion to usespawnwithstdio: 'inherit'. It produces more accurate output thanexecand pipingstdout/stderr, for example when displaying the progress information from agit clone.
– Livven
Apr 18 '17 at 15:56
Thanks, that worked for me.
– Eric
May 24 '15 at 14:56
Thanks, that worked for me.
– Eric
May 24 '15 at 14:56
29
29
Looks like this can be simplified by just using
pipe: coffeeProcess.stdout.pipe(process.stdout);– Eric Freese
Aug 19 '15 at 15:00
Looks like this can be simplified by just using
pipe: coffeeProcess.stdout.pipe(process.stdout);– Eric Freese
Aug 19 '15 at 15:00
3
3
@EricFreese's comment is what I was looking for, because I wanted to leverage stdout's characters replacement feature (harnessing protractor in a node script)
– LoremIpsum
Oct 31 '16 at 11:00
@EricFreese's comment is what I was looking for, because I wanted to leverage stdout's characters replacement feature (harnessing protractor in a node script)
– LoremIpsum
Oct 31 '16 at 11:00
10
10
Simpler:
spawn(cmd, argv, { stdio: 'inherit' }). See nodejs.org/api/child_process.html#child_process_options_stdio for different examples.– Morgan Touverey Quilling
Mar 30 '17 at 14:39
Simpler:
spawn(cmd, argv, { stdio: 'inherit' }). See nodejs.org/api/child_process.html#child_process_options_stdio for different examples.– Morgan Touverey Quilling
Mar 30 '17 at 14:39
2
2
+1 for @MorganTouvereyQuilling's suggestion to use
spawn with stdio: 'inherit'. It produces more accurate output than exec and piping stdout/stderr, for example when displaying the progress information from a git clone.– Livven
Apr 18 '17 at 15:56
+1 for @MorganTouvereyQuilling's suggestion to use
spawn with stdio: 'inherit'. It produces more accurate output than exec and piping stdout/stderr, for example when displaying the progress information from a git clone.– Livven
Apr 18 '17 at 15:56
|
show 2 more comments
There are already several answers however none of them mention the best (and easiest) way to do this, which is using spawn and the { stdio: 'inherit' } option. It seems to produce the most accurate output, for example when displaying the progress information from a git clone.
Simply do this:
var spawn = require('child_process').spawn;
spawn('coffee', ['-cw', 'my_file.coffee'], { stdio: 'inherit' });
Credit to @MorganTouvereyQuilling for pointing this out in this comment.
I found that when the subprocess uses formatted output like colored text,stdio: "inherit"preserves that formatting whilechild.stdout.pipe(process.stdout)does not.
– Rikki Gibson
Sep 21 '17 at 19:59
This perfectly preserves output even on processes with complex output like the progress bars on npm installs. Awesome!
– Dave Koo
Jun 10 '18 at 18:17
add a comment |
There are already several answers however none of them mention the best (and easiest) way to do this, which is using spawn and the { stdio: 'inherit' } option. It seems to produce the most accurate output, for example when displaying the progress information from a git clone.
Simply do this:
var spawn = require('child_process').spawn;
spawn('coffee', ['-cw', 'my_file.coffee'], { stdio: 'inherit' });
Credit to @MorganTouvereyQuilling for pointing this out in this comment.
I found that when the subprocess uses formatted output like colored text,stdio: "inherit"preserves that formatting whilechild.stdout.pipe(process.stdout)does not.
– Rikki Gibson
Sep 21 '17 at 19:59
This perfectly preserves output even on processes with complex output like the progress bars on npm installs. Awesome!
– Dave Koo
Jun 10 '18 at 18:17
add a comment |
There are already several answers however none of them mention the best (and easiest) way to do this, which is using spawn and the { stdio: 'inherit' } option. It seems to produce the most accurate output, for example when displaying the progress information from a git clone.
Simply do this:
var spawn = require('child_process').spawn;
spawn('coffee', ['-cw', 'my_file.coffee'], { stdio: 'inherit' });
Credit to @MorganTouvereyQuilling for pointing this out in this comment.
There are already several answers however none of them mention the best (and easiest) way to do this, which is using spawn and the { stdio: 'inherit' } option. It seems to produce the most accurate output, for example when displaying the progress information from a git clone.
Simply do this:
var spawn = require('child_process').spawn;
spawn('coffee', ['-cw', 'my_file.coffee'], { stdio: 'inherit' });
Credit to @MorganTouvereyQuilling for pointing this out in this comment.
edited May 23 '17 at 11:47
Community♦
11
11
answered Apr 18 '17 at 16:15
LivvenLivven
4,18231719
4,18231719
I found that when the subprocess uses formatted output like colored text,stdio: "inherit"preserves that formatting whilechild.stdout.pipe(process.stdout)does not.
– Rikki Gibson
Sep 21 '17 at 19:59
This perfectly preserves output even on processes with complex output like the progress bars on npm installs. Awesome!
– Dave Koo
Jun 10 '18 at 18:17
add a comment |
I found that when the subprocess uses formatted output like colored text,stdio: "inherit"preserves that formatting whilechild.stdout.pipe(process.stdout)does not.
– Rikki Gibson
Sep 21 '17 at 19:59
This perfectly preserves output even on processes with complex output like the progress bars on npm installs. Awesome!
– Dave Koo
Jun 10 '18 at 18:17
I found that when the subprocess uses formatted output like colored text,
stdio: "inherit" preserves that formatting while child.stdout.pipe(process.stdout) does not.– Rikki Gibson
Sep 21 '17 at 19:59
I found that when the subprocess uses formatted output like colored text,
stdio: "inherit" preserves that formatting while child.stdout.pipe(process.stdout) does not.– Rikki Gibson
Sep 21 '17 at 19:59
This perfectly preserves output even on processes with complex output like the progress bars on npm installs. Awesome!
– Dave Koo
Jun 10 '18 at 18:17
This perfectly preserves output even on processes with complex output like the progress bars on npm installs. Awesome!
– Dave Koo
Jun 10 '18 at 18:17
add a comment |
I'd just like to add that one small issue with outputting the buffer strings from a spawned process with console.log() is that it adds newlines, which can spread your spawned process output over additional lines. If you output stdout or stderr with process.stdout.write() instead of console.log(), then you'll get the console output from the spawned process 'as is'.
I saw that solution here:
Node.js: printing to console without a trailing newline?
Hope that helps someone using the solution above (which is a great one for live output, even if it is from the documentation).
1
For even more accurate output usespawn(command, args, { stdio: 'inherit' }), as suggested by @MorganTouvereyQuilling here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 15:59
add a comment |
I'd just like to add that one small issue with outputting the buffer strings from a spawned process with console.log() is that it adds newlines, which can spread your spawned process output over additional lines. If you output stdout or stderr with process.stdout.write() instead of console.log(), then you'll get the console output from the spawned process 'as is'.
I saw that solution here:
Node.js: printing to console without a trailing newline?
Hope that helps someone using the solution above (which is a great one for live output, even if it is from the documentation).
1
For even more accurate output usespawn(command, args, { stdio: 'inherit' }), as suggested by @MorganTouvereyQuilling here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 15:59
add a comment |
I'd just like to add that one small issue with outputting the buffer strings from a spawned process with console.log() is that it adds newlines, which can spread your spawned process output over additional lines. If you output stdout or stderr with process.stdout.write() instead of console.log(), then you'll get the console output from the spawned process 'as is'.
I saw that solution here:
Node.js: printing to console without a trailing newline?
Hope that helps someone using the solution above (which is a great one for live output, even if it is from the documentation).
I'd just like to add that one small issue with outputting the buffer strings from a spawned process with console.log() is that it adds newlines, which can spread your spawned process output over additional lines. If you output stdout or stderr with process.stdout.write() instead of console.log(), then you'll get the console output from the spawned process 'as is'.
I saw that solution here:
Node.js: printing to console without a trailing newline?
Hope that helps someone using the solution above (which is a great one for live output, even if it is from the documentation).
edited May 23 '17 at 12:10
Community♦
11
11
answered Jul 29 '14 at 11:54
Kevin TeljeurKevin Teljeur
1,2501013
1,2501013
1
For even more accurate output usespawn(command, args, { stdio: 'inherit' }), as suggested by @MorganTouvereyQuilling here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 15:59
add a comment |
1
For even more accurate output usespawn(command, args, { stdio: 'inherit' }), as suggested by @MorganTouvereyQuilling here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 15:59
1
1
For even more accurate output use
spawn(command, args, { stdio: 'inherit' }), as suggested by @MorganTouvereyQuilling here stackoverflow.com/questions/10232192/…– Livven
Apr 18 '17 at 15:59
For even more accurate output use
spawn(command, args, { stdio: 'inherit' }), as suggested by @MorganTouvereyQuilling here stackoverflow.com/questions/10232192/…– Livven
Apr 18 '17 at 15:59
add a comment |
Inspired by Nathanael Smith's answer and Eric Freese's comment, it could be as simple as:
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee').stdout.pipe(process.stdout);
This seems to work fine for simple commands likelsbut fails for more complex commands such asnpm install. I even tried piping both stdout and stderr to their respective process objects.
– linuxdan
Aug 19 '16 at 15:15
@linuxdan it may be because npm is writing in stderr (i saw some write the progress bar there). you can pipe also stderr, or extend Tongfa solution to listen on stderr.
– Sergiu
Jan 16 '17 at 17:10
@linuxdan From what I've seen the most reliable way isspawn(command, args, { stdio: 'inherit' }), as suggested here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 16:03
add a comment |
Inspired by Nathanael Smith's answer and Eric Freese's comment, it could be as simple as:
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee').stdout.pipe(process.stdout);
This seems to work fine for simple commands likelsbut fails for more complex commands such asnpm install. I even tried piping both stdout and stderr to their respective process objects.
– linuxdan
Aug 19 '16 at 15:15
@linuxdan it may be because npm is writing in stderr (i saw some write the progress bar there). you can pipe also stderr, or extend Tongfa solution to listen on stderr.
– Sergiu
Jan 16 '17 at 17:10
@linuxdan From what I've seen the most reliable way isspawn(command, args, { stdio: 'inherit' }), as suggested here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 16:03
add a comment |
Inspired by Nathanael Smith's answer and Eric Freese's comment, it could be as simple as:
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee').stdout.pipe(process.stdout);
Inspired by Nathanael Smith's answer and Eric Freese's comment, it could be as simple as:
var exec = require('child_process').exec;
exec('coffee -cw my_file.coffee').stdout.pipe(process.stdout);
answered Jan 18 '16 at 10:39
Tyler LongTyler Long
10.6k66964
10.6k66964
This seems to work fine for simple commands likelsbut fails for more complex commands such asnpm install. I even tried piping both stdout and stderr to their respective process objects.
– linuxdan
Aug 19 '16 at 15:15
@linuxdan it may be because npm is writing in stderr (i saw some write the progress bar there). you can pipe also stderr, or extend Tongfa solution to listen on stderr.
– Sergiu
Jan 16 '17 at 17:10
@linuxdan From what I've seen the most reliable way isspawn(command, args, { stdio: 'inherit' }), as suggested here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 16:03
add a comment |
This seems to work fine for simple commands likelsbut fails for more complex commands such asnpm install. I even tried piping both stdout and stderr to their respective process objects.
– linuxdan
Aug 19 '16 at 15:15
@linuxdan it may be because npm is writing in stderr (i saw some write the progress bar there). you can pipe also stderr, or extend Tongfa solution to listen on stderr.
– Sergiu
Jan 16 '17 at 17:10
@linuxdan From what I've seen the most reliable way isspawn(command, args, { stdio: 'inherit' }), as suggested here stackoverflow.com/questions/10232192/…
– Livven
Apr 18 '17 at 16:03
This seems to work fine for simple commands like
ls but fails for more complex commands such as npm install. I even tried piping both stdout and stderr to their respective process objects.– linuxdan
Aug 19 '16 at 15:15
This seems to work fine for simple commands like
ls but fails for more complex commands such as npm install. I even tried piping both stdout and stderr to their respective process objects.– linuxdan
Aug 19 '16 at 15:15
@linuxdan it may be because npm is writing in stderr (i saw some write the progress bar there). you can pipe also stderr, or extend Tongfa solution to listen on stderr.
– Sergiu
Jan 16 '17 at 17:10
@linuxdan it may be because npm is writing in stderr (i saw some write the progress bar there). you can pipe also stderr, or extend Tongfa solution to listen on stderr.
– Sergiu
Jan 16 '17 at 17:10
@linuxdan From what I've seen the most reliable way is
spawn(command, args, { stdio: 'inherit' }), as suggested here stackoverflow.com/questions/10232192/…– Livven
Apr 18 '17 at 16:03
@linuxdan From what I've seen the most reliable way is
spawn(command, args, { stdio: 'inherit' }), as suggested here stackoverflow.com/questions/10232192/…– Livven
Apr 18 '17 at 16:03
add a comment |
After reviewing all the other answers, I ended up with this:
function oldSchoolMakeBuild(cb) {
var makeProcess = exec('make -C ./oldSchoolMakeBuild',
function (error, stdout, stderr) {
stderr && console.error(stderr);
cb(error);
});
makeProcess.stdout.on('data', function(data) {
process.stdout.write('oldSchoolMakeBuild: '+ data);
});
}
Sometimes data will be multiple lines, so the oldSchoolMakeBuild header will appear once for multiple lines. But this didn't bother me enough to change it.
add a comment |
After reviewing all the other answers, I ended up with this:
function oldSchoolMakeBuild(cb) {
var makeProcess = exec('make -C ./oldSchoolMakeBuild',
function (error, stdout, stderr) {
stderr && console.error(stderr);
cb(error);
});
makeProcess.stdout.on('data', function(data) {
process.stdout.write('oldSchoolMakeBuild: '+ data);
});
}
Sometimes data will be multiple lines, so the oldSchoolMakeBuild header will appear once for multiple lines. But this didn't bother me enough to change it.
add a comment |
After reviewing all the other answers, I ended up with this:
function oldSchoolMakeBuild(cb) {
var makeProcess = exec('make -C ./oldSchoolMakeBuild',
function (error, stdout, stderr) {
stderr && console.error(stderr);
cb(error);
});
makeProcess.stdout.on('data', function(data) {
process.stdout.write('oldSchoolMakeBuild: '+ data);
});
}
Sometimes data will be multiple lines, so the oldSchoolMakeBuild header will appear once for multiple lines. But this didn't bother me enough to change it.
After reviewing all the other answers, I ended up with this:
function oldSchoolMakeBuild(cb) {
var makeProcess = exec('make -C ./oldSchoolMakeBuild',
function (error, stdout, stderr) {
stderr && console.error(stderr);
cb(error);
});
makeProcess.stdout.on('data', function(data) {
process.stdout.write('oldSchoolMakeBuild: '+ data);
});
}
Sometimes data will be multiple lines, so the oldSchoolMakeBuild header will appear once for multiple lines. But this didn't bother me enough to change it.
answered Nov 11 '16 at 17:15
TongfaTongfa
97797
97797
add a comment |
add a comment |
I have found it helpful to add a custom exec script to my utilities that does this.
utilities.js
const { exec } = require('child_process')
module.exports.exec = (command) => {
const process = exec(command)
process.stdout.on('data', (data) => {
console.log('stdout: ' + data.toString())
})
process.stderr.on('data', (data) => {
console.log('stderr: ' + data.toString())
})
process.on('exit', (code) => {
console.log('child process exited with code ' + code.toString())
})
}
app.js
const { exec } = require('./utilities.js)
exec('coffee -cw my_file.coffee')
add a comment |
I have found it helpful to add a custom exec script to my utilities that does this.
utilities.js
const { exec } = require('child_process')
module.exports.exec = (command) => {
const process = exec(command)
process.stdout.on('data', (data) => {
console.log('stdout: ' + data.toString())
})
process.stderr.on('data', (data) => {
console.log('stderr: ' + data.toString())
})
process.on('exit', (code) => {
console.log('child process exited with code ' + code.toString())
})
}
app.js
const { exec } = require('./utilities.js)
exec('coffee -cw my_file.coffee')
add a comment |
I have found it helpful to add a custom exec script to my utilities that does this.
utilities.js
const { exec } = require('child_process')
module.exports.exec = (command) => {
const process = exec(command)
process.stdout.on('data', (data) => {
console.log('stdout: ' + data.toString())
})
process.stderr.on('data', (data) => {
console.log('stderr: ' + data.toString())
})
process.on('exit', (code) => {
console.log('child process exited with code ' + code.toString())
})
}
app.js
const { exec } = require('./utilities.js)
exec('coffee -cw my_file.coffee')
I have found it helpful to add a custom exec script to my utilities that does this.
utilities.js
const { exec } = require('child_process')
module.exports.exec = (command) => {
const process = exec(command)
process.stdout.on('data', (data) => {
console.log('stdout: ' + data.toString())
})
process.stderr.on('data', (data) => {
console.log('stderr: ' + data.toString())
})
process.on('exit', (code) => {
console.log('child process exited with code ' + code.toString())
})
}
app.js
const { exec } = require('./utilities.js)
exec('coffee -cw my_file.coffee')
answered Oct 7 '17 at 6:26
IanLancasterIanLancaster
412
412
add a comment |
add a comment |
child_process.spawn returns an object with stdout and stderr streams.
You can tap on the stdout stream to read data that the child process sends back to Node. stdout being a stream has the "data", "end", and other events that streams have. spawn is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc.
so you can solve your problem using child_process.spawn as used below.
var spawn = require('child_process').spawn,
ls = spawn('coffee -cw my_file.coffee');
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('code ' + code.toString());
});
add a comment |
child_process.spawn returns an object with stdout and stderr streams.
You can tap on the stdout stream to read data that the child process sends back to Node. stdout being a stream has the "data", "end", and other events that streams have. spawn is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc.
so you can solve your problem using child_process.spawn as used below.
var spawn = require('child_process').spawn,
ls = spawn('coffee -cw my_file.coffee');
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('code ' + code.toString());
});
add a comment |
child_process.spawn returns an object with stdout and stderr streams.
You can tap on the stdout stream to read data that the child process sends back to Node. stdout being a stream has the "data", "end", and other events that streams have. spawn is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc.
so you can solve your problem using child_process.spawn as used below.
var spawn = require('child_process').spawn,
ls = spawn('coffee -cw my_file.coffee');
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('code ' + code.toString());
});
child_process.spawn returns an object with stdout and stderr streams.
You can tap on the stdout stream to read data that the child process sends back to Node. stdout being a stream has the "data", "end", and other events that streams have. spawn is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc.
so you can solve your problem using child_process.spawn as used below.
var spawn = require('child_process').spawn,
ls = spawn('coffee -cw my_file.coffee');
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('code ' + code.toString());
});
answered May 15 '18 at 7:06
Adeojo Emmanuel IMMAdeojo Emmanuel IMM
632516
632516
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f10232192%2fexec-display-stdout-live%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I came here looking for capturing stdout from Python executable. Note that all of the below will work, but you need to run python with a "-u" option, to make outout unbuffered and thereby have live updates.
– Andy
Nov 5 '17 at 18:36