Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8679979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:07:54+00:00 2026-06-12T21:07:54+00:00

I am a newbie as far as node.js is concerned.I wrote the following code

  • 0

I am a newbie as far as node.js is concerned.I wrote the following code to pipeline two linux commands.
This is my nodejs code:

var spawn = require('child_process').spawn,
    ls = spawn('ls',['-lh','/usr']),
    grep = spawn('grep',['bin']);

/*
ls.stdout.on('data',function(data){
    console.log('stdout: '+data);
});
*/

ls.stdout.on('data',function(data){
    console.log(""+data);
    grep.stdin.write(data);
});

ls.stderr.on('data',function(data){
    console.log('stderr: '+data);
});


ls.on('exit',function(code){
    console.log('Exit code '+code);
    grep.stdin.end();
})

// ------------------------------------

grep.stdout.on('data',function(data){
    console.log('stdout: '+data);
});

grep.stderr.on('data',function(data){
    console.log('stderr: '+data);
});

Now this code fails sometimes and runs sometimes.I’m getting confused now.
When it fails,it says:

Exit code 0
total 160K
drwxr-xr-x   2 root root  68K Oct 12 12:54 bin
drwxr-xr-x   2 root root 4.0K Jun 20 19:58 games
drwxr-xr-x  54 root root 4.0K Sep 24 17:52 include
drwxr-xr-x 252 root root  44K Oct  2 21:53 lib
drwxr-xr-x  10 root root 4.0K Apr 28 19:16 local
drwxr-xr-x   2 root root  12K Sep 18 15:51 sbin
drwxr-xr-x 362 root root  12K Sep 28 17:58 share
drwxr-xr-x   5 root root 4.0K Jul  7 23:39 src


events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: This socket is closed.
    at Socket._write (net.js:517:19)
    at Socket.write (net.js:509:15)
    at Socket.<anonymous> (/home/rajat/nodexperiments/full-spawn.js:13:13)
    at Socket.EventEmitter.emit (events.js:88:17)
    at Pipe.onread (net.js:395:14)

And when it runs,it says:

total 160K
drwxr-xr-x   2 root root  68K Oct 12 12:54 bin
drwxr-xr-x   2 root root 4.0K Jun 20 19:58 games
drwxr-xr-x  54 root root 4.0K Sep 24 17:52 include
drwxr-xr-x 252 root root  44K Oct  2 21:53 lib
drwxr-xr-x  10 root root 4.0K Apr 28 19:16 local
drwxr-xr-x   2 root root  12K Sep 18 15:51 sbin
drwxr-xr-x 362 root root  12K Sep 28 17:58 share
drwxr-xr-x   5 root root 4.0K Jul  7 23:39 src

Exit code 0
stdout: drwxr-xr-x   2 root root  68K Oct 12 12:54 bin
drwxr-xr-x   2 root root  12K Sep 18 15:51 sbin

Any Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:07:57+00:00Added an answer on June 12, 2026 at 9:07 pm

    This is quite clearly a race condition, because node.js is a highly parallel environment you have found a really nice example to demonstrate this.

    ls.on('exit',function(code){
        console.log('Exit code '+code);
        grep.stdin.end();
    })
    

    ls fires the above event, before it has finished writing to grep, and then closes the socket that’s being used to communicate. The hint your output gives you is that the exit(0) message coming from ls appears once at the top of the output, and once just above the error message.

    You shouldn’t be closing grep’s stdin channel here.

    But what about using .exec() instead of spawn on a bash script that does something like

    #/bin/bash mybashscript
    ls $1 | grep bin
    

    and then using the JavaScript closure, such as

    child = exec('mybashscript',['-lh','/usr']
      function (error, stdout, stderr) {
        console.log('stdout: ' + stdout);
        console.log('stderr: ' + stderr);
        if (error !== null) {
          console.log('exec error: ' + error);
        }
    });
    

    With the events your code ordering is more difficult to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a complete newbie as far as jQuery is concerned. I also don't know
I am newbie as far as developing chrome extension is concerned. I am testing
I have the following form in a html5 document.I am a newbie as far
I am a newbie as far as open-source development is concerned. I have tried
I'm a newbie as far as Java UI development is concerned. I was initially
I'm a newbie to programming for iPhone, I'm following along this book. I'm stuck
I'm a newbie at this so I'll explain what I did so far: Open
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
Two seemingly identical queries (as far as a newbie like me can tell, but
I will begin by saying that I'm a complete newbie as far as AppleScript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.