Node: How to control the output of a child process

Source: Internet
Author: User

As you know, in a node program, if the current process wants to generate a child process, it can invoke the Spawn method of the Child_process module. The Spawn method signature is as follows:

Child_process.spawn (command[, args][, Options])

The properties of the options object stdio are used to control the output of the child process.

When the Options.stdio is set to inherit, the output of the child process is redirected to the StdOut object of the current process, which means that the output of the child process is displayed directly in the current process's control

System.

When the Options.stdio is set to pipe, the output of the child process is redirected to the StdOut object of the Spawn method's return value. The situation is slightly more complicated. I'll give you one of these.

Examples of scenarios. If we want to write the version number of node to a file in the current process, we can write the following code:

1 varFS = require (' FS ');2 varPath = require (' path ');3 var{Spawn} = require (' child_process '));4 5 varChild = Spawn (' node ', ['--version ')], {6Stdio: 'Pipe‘7 });8 9 varFilePath = Path.resolve (' node-version.txt '));Ten varDestination =Fs.createwritestream (filePath); OneChild.stdout.pipe (destination);

The output of the child process is redirected to Child.stdout, Child.stdout is a readable stream, so the data can be written to the final file using its pipe method.

Node: How to control the output of a child process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.