Node. js child_process exec return value truncated

Source: Internet
Author: User

Node. js child_process exec return value truncated
In practice, a problem is encountered. When the system command is called using child_process exec, if the returned content of the command is large, the obtained stdout will be truncated. The following is the relevant code:

  1. Exec ("some command", function (error, stdout, stderr ){
  2. // Use the returned value stdout for related Calculation
  3. }

After investigation, we found that a buffer zone will be used during exec execution. The default size is 200 × 1024. So the solution is to increase the buffer size to the appropriate value:

  1. Exec ('dir/B/O-D ^ 2014 *',{
  2. MaxBuffer: 2000*1024 // quick fix
  3. }, Function (error, stdout, stderr ){
  4. List_of_filenames = stdout. split ('\ r \ n'); // adapt to your line ending char
  5. Console. log ("Found % s files in the replay folder", list_of_filenames.length)
  6. }
  7. )
For more information, see the official documentation.

Others also provide a solution, but it is not verified for reference only:

  1. Var exec = require('child_process'cmd.exe c;

  2. Function my_exec (command, callback ){
  3. Var proc = exec (command );

  4. Var list = [];
  5. Proc. stdout. setEncoding ('utf8 ');

  6. Proc. stdout. on ('data', function (chunk ){
  7. List. push (chunk );
  8. });

  9. Proc. stdout. on ('end', function (){
  10. Callback (list. join ());
  11. });
  12. }

  13. My_exec ('dir/s', function (stdout ){
  14. Console. log (stdout );
  15. })


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.