Sometimes you may need to hang a Node.js process then run your own process cleanup.
In my case I wanted to prevent a Gulp task from exiting until the user exited by pressing ctrl-c I wanted to run a selenium server then clean up and shutdown the server on exit.

gulp.task("dev-server", ["proxy", "selenium"], (done) => {
    process.stdin.resume(); //hang the process, strange api but works
    console.log("Press \"Ctrl + C\" to exit");
    process.on("exit", (err) => {
        if(err) console.log(err);
        cleanup(proxyServer, seleniumServer); // this is an external function
        done();
        process.exit();
    });
});

This line hangs the process: process.stdin.resume() until you run the final exit command at a later stage: process.exit();

If you would like listen to the system event that signals a quite i.e. ctrl-c you can listen to the this event:

process.on("exit", (err) => {
    if(err) console.log(err);
    process.exit();
});
 

Ryan is a hacker dad living in Cape Town, South Africa who likes Skate Boarding JavaScript and Functional Programming.

ryanwild @javascript_fn