【问题标题】:nodemon stopping doesn't stop the process in ubuntunodemon 停止不会停止 ubuntu 中的进程
【发布时间】:2021-01-08 21:56:56
【问题描述】:

我在我的 nodejs 应用程序中使用 nodemon 来在应用更改时自动重启。但是当我在ubuntu环境中使用'Ctrl + C'停止nodemon时,不会停止nodejs。我必须搜索从端口运行的进程,并且必须使用 kill -9 手动终止。我该如何解决这个问题?

【问题讨论】:

    标签: node.js ubuntu-16.04


    【解决方案1】:

    快速而肮脏的解决方案

    process.on('SIGTERM', stopHandler);
    process.on('SIGINT', stopHandler);
    process.on('SIGHUP', stopHandler);
    function stopHandler() {
      console.log('Stopped forcefully');
      process.exit(0);
    }
    

    正确的解决方案

    实施Graceful Shutdown 是最佳实践。在这个例子中,我应该只停止服务器。如果服务器停止的时间超过 2 秒,则进程将以退出代码 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][1] 的回答非常好。非高级用户的唯一问题是终端被挂起并且在您杀死前一个进程之前不会响应,您可能不想全部杀死它们,而只想杀死造成冲突的那个。为此,请执行以下操作: 1.- 通过搜索正在侦听端口的进程来查找进程 ID:netstat -ltnp | grep -w ':PORT' 然后kill -9 ProcessID [1]:*.com/users/4850916/galkin
    最近更新 更多