I'd like to restart gulp
on certain changes. That can be easily done by placing the following within the gulpfile
spawn('gulp', [], { stdio: 'inherit'});
However, once gulp
restarts in this way, the process is no longer killed properly with Ctrl+C via the terminal. If I start gulp
via terminal, I can capture a Ctrl+C signal, but can't if gulp
was started via the spawn
in gulpfile
. How can I capture 'SIGINT' for the spawn?
Okay here's the full story to anyone who might encounter the issue. From what I have been reading, whenever you want to restart gulp from within gulp you simply use:
spawn('gulp', [], { stdio: 'inherit'});
process.exit();
I didn't mention process.exit()
in my question as I didn't expect it to affect the usage of Ctrl+C. Indeed it was, as my server was an ExpressJS one, whenever I'd use Ctrl+C after gulp restarted from within itself, I would get the port still in use error (Error: listen EADDRINUSE
). Obviously, all node processes wasn't being closed. Once I removed the line process.exit()
from my code, I was able to use Ctrl+C and successfully close all processes. Below is the useful bit of code in the gulpfile and output in terminal that is related to this issue.
// gulpfile.js
gulp.task('restart', function() {
server.close();
spawn('gulp', [], { stdio: 'inherit'});
process.exit(); // this line needs to be removed
});
process.on('SIGINT', function() {
setTimeout(function() {
gutil.log(gutil.colors.red('Successfully closed ' + process.pid));
process.exit(1);
}, 500);
});
// Console results:
^C[20:12:12] Successfully closed 67160
[20:12:12] Successfully closed 67151