博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何从Node.js程序退出
阅读量:2505 次
发布时间:2019-05-11

本文共 3536 字,大约阅读时间需要 11 分钟。

There are various ways to terminate a Node.js application.

有多种方法可以终止Node.js应用程序。

When running a program in the console you can close it with ctrl-C, but what I want to discuss here is programmatically exiting.

在控制台中运行程序时,可以使用ctrl-C其关闭,但是我想在这里讨论以编程方式退出。

Let’s start with the most drastic one, and see why you’re better off not using it.

让我们从最激烈的话题开始,看看为什么使用它会更好。

The process core module is provides a handy method that allows you to programmatically exit from a Node.js program: process.exit().

process核心模块提供了一种方便的方法,使您可以以编程方式退出Node.js程序: process.exit()

When Node.js runs this line, the process is immediately forced to terminate.

当Node.js运行此行时,该过程立即被迫终止。

This means that any callback that’s pending, any network request still being sent, any filesystem access, or processes writing to stdout or stderr - all is going to be ungracefully terminated right away.

这意味着任何未决的回调,任何仍在发送的网络请求,任何文件系统访问或写入stdoutstderr进程-所有这些都将立即被恶意终止。

If this is fine for you, you can pass an integer that signals the operating system the exit code:

如果适合您,则可以传递一个整数,向操作系统发出退出代码:

process.exit(1)

By default the exit code is 0, which means success. Different exit codes have different meaning, which you might want to use in your own system to have the program communicate to other programs.

默认情况下,退出代码为0 ,表示成功。 不同的退出代码具有不同的含义,您可能希望在自己的系统中使用这些退出代码,以使程序与其他程序进行通信。

You can read more on exit codes at

您可以在上阅读有关退出代码的更多信息。

You can also set the process.exitCode property:

您还可以设置process.exitCode属性:

process.exitCode = 1

and when the program will later end, Node will return that exit code.

当程序稍后结束时,Node将返回该退出代码。

A program will gracefully exit when all the processing is done.

完成所有处理后,程序将正常退出。

Many times with Node we start servers, like this HTTP server:

我们使用Node多次启动服务器,例如HTTP服务器:

const express = require('express')const app = express()app.get('/', (req, res) => {  res.send('Hi!')})app.listen(3000, () => console.log('Server ready'))

This program is never going to end. If you call process.exit(), any currently pending or running request is going to be aborted. This is not nice.

这个程序永远不会结束。 如果您调用process.exit() ,那么任何当前挂起或正在运行的请求都将被中止。 这不好

In this case you need to send the command a SIGTERM signal, and handle that with the process signal handler:

在这种情况下,您需要向该命令发送SIGTERM信号,并使用过程信号处理程序进行处理:

Note: process does not require a “require”, it’s automatically available.

注意: process不需要“要求”,它是自动可用的。

const express = require('express')const app = express()app.get('/', (req, res) => {  res.send('Hi!')})const server = app.listen(3000, () => console.log('Server ready'))process.on('SIGTERM', () => {  server.close(() => {    console.log('Process terminated')  })})

What are signals? Signals are a POSIX intercommunication system: a notification sent to a process in order to notify it of an event that occurred.

什么是信号? 信号是POSIX互通系统:通知发送到流程,以便将发生的事件通知给流程。

SIGKILL is the signals that tells a process to immediately terminate, and would ideally act like process.exit().

SIGKILL是告诉进程立即终止的信号,理想情况下,其行为类似于process.exit()

SIGTERM is the signals that tells a process to gracefully terminate. It is the signal that’s sent from process managers like upstart or supervisord and many others.

SIGTERM是告诉进程正常终止的信号。 它是从流程管理者(例如upstartsupervisord以及其他许多人发出的信号。

You can send this signal from inside the program, in another function:

您可以在另一个函数中从程序内部发送此信号:

process.kill(process.pid, 'SIGTERM')

Or from another Node.js running program, or any other app running in your system that knows the PID of the process you want to terminate.

或从另一个正在运行的Node.js程序,或从系统中运行的其他任何知道您要终止的进程的PID的应用程序。

翻译自:

转载地址:http://coqgb.baihongyu.com/

你可能感兴趣的文章
获取设备实际宽度
查看>>
Notes on <High Performance MySQL> -- Ch3: Schema Optimization and Indexing
查看>>
Kafka的安装和配置
查看>>
Alpha冲刺(10/10)
查看>>
数组Array的API2
查看>>
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>
spfile
查看>>
Team Foundation Service更新:改善了导航和项目状态速查功能
查看>>
0x13 链表与邻接表
查看>>
js封装设置获取cookie
查看>>
bzoj 1002 [FJOI2007]轮状病毒 Matrix-Tree定理+递推
查看>>
C#面向对象模式设计第十四讲:Template Method 模板模式(行为型模式)
查看>>
linux后台运行命令:&和nohup
查看>>
springboot + shiro学习(配置SecurityManager,Realm)
查看>>
iOS中使用nil NULL NSNULL的区别
查看>>
Hdu1754-线段树-单点更新
查看>>
asp.net mvc 4.0的部署
查看>>
WordPress资源站点推荐
查看>>