0
我正在使用我的nodejs應用程序中的nodemon在應用更改時自動重新啓動。但是,當我在ubuntu環境中使用'Ctrl + C'來停止nodemon時,不會停止nodejs。我必須搜索從端口運行的進程,並且必須使用kill -9手動殺死。我怎樣才能解決這個問題?nodemon停止不會停止ubuntu中的進程
我正在使用我的nodejs應用程序中的nodemon在應用更改時自動重新啓動。但是,當我在ubuntu環境中使用'Ctrl + C'來停止nodemon時,不會停止nodejs。我必須搜索從端口運行的進程,並且必須使用kill -9手動殺死。我怎樣才能解決這個問題?nodemon停止不會停止ubuntu中的進程
快速和骯髒的解決方案
process.on('SIGTERM', stopHandler);
process.on('SIGINT', stopHandler);
process.on('SIGHUP', stopHandler);
function stopHandler() {
console.log('Stopped forcefully');
process.exit(0);
}
正確的解決方案
實現Graceful Shutdown
是最佳的做法。在這個例子中,我應該只停止服務器。如果服務器的停止時間超過2s,則該過程將終止,並退出代碼1
。
process.on('SIGTERM', stopHandler);
process.on('SIGINT', stopHandler);
process.on('SIGHUP', stopHandler);
async function stopHandler() {
console.log('Stopping...');
const timeoutId = setTimeout(() => {
process.exit(1);
console.error('Stopped forcefully, not all connection was closed');
}, 2000);
try {
await server.stop();
clearTimeout(timeoutId);
} catch (error) {
console.error(error, 'Error during stop.');
process.exit(1);
}
}
是不是有任何快捷鍵或什麼可以做到這一點?我的意思是按'Ctrl + C'? –
CTRL + C將發送信號SIGINT – galkin