how to close stdin?

10,173 views
Skip to first unread message

AJ ONeal

unread,
Jan 20, 2012, 5:56:49 PM1/20/12
to node.js mailing list
stdin = process.openStdin() // that's fun
stdin.end() // hmm... not as cool

How do I close stdin without waiting for ctrl+d?

AJ ONeal

Nathan Rajlich

unread,
Jan 20, 2012, 6:24:37 PM1/20/12
to nod...@googlegroups.com
Why do you need to close stdin? Seems like an unconventional thing to do. But this *might* work:

  fs.closeSync(0);

Side note: you can just access `process.stdin` directly (for a while) now.


AJ ONeal

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

AJ ONeal

unread,
Jan 20, 2012, 7:42:13 PM1/20/12
to nod...@googlegroups.com
I'm trying to implement this common cli usage:

node dostuff.js /to/a/file.ext
echo 'blah' | node dostuff.js
node dostuff.js
> type or paste it in

And I want it so that unlike grep and other apps that "leave you hangin'" it will auto close and display the usage after 3 seconds of no input.

AJ ONeal

Scott González

unread,
Jan 20, 2012, 7:54:27 PM1/20/12
to nod...@googlegroups.com
I use process.stdin.destroy(). I wish there were a way to put stdin back to its original state, which is paused, but allows Node to exit when nothing else is waiting...



AJ ONeal

--

Scott González

unread,
Jan 20, 2012, 7:56:05 PM1/20/12
to nod...@googlegroups.com
What version of node are you using? You should be opening stdin with process.stdin.resume();


AJ ONeal

--

Ben Noordhuis

unread,
Jan 20, 2012, 8:33:16 PM1/20/12
to nod...@googlegroups.com

process.stdin.pause()

Scott González

unread,
Jan 20, 2012, 8:40:23 PM1/20/12
to nod...@googlegroups.com
On Fri, Jan 20, 2012 at 8:33 PM, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
process.stdin.pause()

If that's how you close stdin, why does this script never exit?

process.stdin.resume();
process.stdin.pause();

Ben Noordhuis

unread,
Jan 20, 2012, 8:50:31 PM1/20/12
to nod...@googlegroups.com
2012/1/21 Scott González <scott.g...@gmail.com>:

You need to upgrade, that bug was fixed in v0.6.6.

Scott González

unread,
Jan 20, 2012, 8:58:04 PM1/20/12
to nod...@googlegroups.com
On Fri, Jan 20, 2012 at 8:50 PM, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
You need to upgrade, that bug was fixed in v0.6.6.

That makes more sense. Thanks.

AJ ONeal

unread,
Jan 21, 2012, 12:28:56 AM1/21/12
to nod...@googlegroups.com
`stdin.resume()` and `stdin.pause()` work for me.

I don't believe that the latest documentation mentions this.

AJ ONeal


2012/1/20 Scott González <scott.g...@gmail.com>
On Fri, Jan 20, 2012 at 8:50 PM, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
You need to upgrade, that bug was fixed in v0.6.6.

That makes more sense. Thanks.

--

Ben Noordhuis

unread,
Jan 21, 2012, 7:54:19 AM1/21/12
to nod...@googlegroups.com
On Sat, Jan 21, 2012 at 06:28, AJ ONeal <cool...@gmail.com> wrote:
> `stdin.resume()` and `stdin.pause()` work for me.
>
> I don't believe that the latest documentation mentions this.

"A Readable Stream for stdin. The stdin stream is paused by default,
so one must call process.stdin.resume() to read from it."[1]

I'm open to suggestions if you think it could be phrased better.

[1] http://nodejs.org/docs/v0.6.8/api/process.html#process.stdin

AJ ONeal

unread,
Jan 22, 2012, 10:07:58 PM1/22/12
to nod...@googlegroups.com
I understand that since it inherits from stream, one should know that `pause` is how to "close" it, but for the sake of clarity for people who haven't had enough of their coffee or cocoa yet that day I would suggest adding:

`process.stdin.pause` will "close" `stdin`.

AJ ONeal

Reply all
Reply to author
Forward
0 new messages