I'm trying to write a gulp task to execute a program and to wait for any key to be pressed to exit the program.
So far I have this:
gulp.task('runDevelopment', function (done) {
console.log('\n\n---\nPress any key to exit\n---\n');
var api = spawn('node', ['node_modules/api-gateway-server/server.js', 'etc/swagger.yaml']);
api.stdout.on('data', function (data) {
console.log(data.toString());
});
api.stderr.on('data', function (data) {
console.log(data.toString());
});
process.stdin.on('data', function () {
api.kill('SIGKILL');
done();
});
});
This runs the program, and kills it as expected but gulp never quits. I see this in the console:
20:04 $ gulp
[20:06:54] Using gulpfile ~/Development/swiki/gulpfile.js
[20:06:54] Starting 'documentCopy'...
[20:06:54] Starting 'documentZip'...
[20:06:54] Starting 'runDevelopment'...
---
Press any key to exit
---
[20:06:54] Finished 'documentCopy' after 52 ms
[20:06:54] Finished 'documentZip' after 41 ms
[20:06:54] Starting 'package'...
[20:06:54] Finished 'package' after 4.9 μs
API Gateway server listening on port 7111
[20:06:57] Finished 'runDevelopment' after 3.06 s
[20:06:57] Starting 'run'...
[20:06:57] Finished 'run' after 2.72 μs
[20:06:57] Starting 'default'...
[20:06:57] Finished 'default' after 1.16 μs
Terminated: 15
✘-TERM ~/Development/swiki [master|…6]
20:07 $
gulp gives me the Terminated: 15
only after I killall gulp
in another terminal.
How can I get this to work properly?
The problem is that you're successfully terminating the child process by sending SIGKILL
, but you're still listening for incoming data on process.stdin
of the parent process. As long as you are doing that the parent process won't exit.
You have to explicitly tell process.stdin
to no longer emit 'data'
events by calling stream.pause()
:
process.stdin.on('data', function () {
process.stdin.pause();
api.kill('SIGKILL');
done();
});
If all else fails you can also call process.exit()
as a last resort:
process.stdin.on('data', function () {
api.kill('SIGKILL');
done();
process.exit(0);
});