Setting exit code without calling process.exit()

1,220 views
Skip to first unread message

Will Conant

unread,
Dec 5, 2011, 4:14:19 PM12/5/11
to nod...@googlegroups.com
Is it possible to set the exit code without calling process.exit? I find myself writing scripts that just run until the event loop is empty. Along the way, they might find error conditions and log them to STDERR. It would be convenient if I could decide what the exit code should be right when I find an error, rather than having to track all my callbacks and then call process.exit when everything is done. Any ideas?


Thanks!
Will

Jann Horn

unread,
Dec 5, 2011, 4:17:56 PM12/5/11
to nod...@googlegroups.com
2011/12/5 Will Conant <will....@gmail.com>:

Try `process.on('exit', function() { process.exit(currentErrorcode) })`

Will Conant

unread,
Dec 5, 2011, 4:28:31 PM12/5/11
to nod...@googlegroups.com
That loops infinitely, but it suddenly occurs to me that I could keep track of the fact that I called process.exit in the exit event and only do it once. Let me try that.

Will Conant

unread,
Dec 5, 2011, 4:31:56 PM12/5/11
to nod...@googlegroups.com
Yeah. That does the trick.

Matt

unread,
Dec 5, 2011, 4:54:15 PM12/5/11
to nod...@googlegroups.com
process.once() instead?

On Mon, Dec 5, 2011 at 4:31 PM, Will Conant <will....@gmail.com> wrote:
Yeah. That does the trick.

--
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

mscdex

unread,
Dec 5, 2011, 5:04:50 PM12/5/11
to nodejs
On Dec 5, 4:17 pm, Jann Horn <jannh...@googlemail.com> wrote:
> Try `process.on('exit', function() { process.exit(currentErrorcode) })`

You should use process.reallyExit(currentErrorcode) instead of
process.exit(currentErrorcode) here.

Will Conant

unread,
Dec 5, 2011, 5:10:19 PM12/5/11
to nod...@googlegroups.com
Even better. Thanks!

Ben Noordhuis

unread,
Dec 5, 2011, 5:26:05 PM12/5/11
to nod...@googlegroups.com

I fixed process.exit(), it's now safe to call process.exit() from an
exit listener.

https://github.com/joyent/node/commit/36815e4

Reply all
Reply to author
Forward
0 new messages