# process
`process`對(duì)象是一個(gè)全局對(duì)象,可以在任何地方訪問(wèn)到它。 它是[EventEmitter](#)的一個(gè)實(shí)例。
### Exit Codes
Node 執(zhí)行程序正常情況下會(huì)返回 0,這也意味著,包括所有“異步”在內(nèi)的操作都已結(jié)束。(筆者注:linux terminal 下使用 echo $? 查看,win cmd 下使用 echo %ERRORLEVEL% 查看)除此之外的其他返回狀態(tài)如下:
- `1`**未捕獲的致命異常(Uncaught Fatal Exception)** - There was an uncaught exception, and it was not handled by a domain or an `uncaughtException` event handler.
- `2` - 未使用(Unused) (reserved by Bash for builtin misuse)
- `3`**解析錯(cuò)誤(Internal JavaScript Parse Error)** - The JavaScript source code internal in Node's bootstrapping process caused a parse error. This is extremely rare, and generally can only happen during development of Node itself.
- `4`**評(píng)估失敗(Internal JavaScript Evaluation Failure)** - The JavaScript source code internal in Node's bootstrapping process failed to return a function value when evaluated. This is extremely rare, and generally can only happen during development of Node itself.
- `5`**致命錯(cuò)誤(Fatal Error)** - There was a fatal unrecoverable error in V8. Typically a message will be printed to stderr with the prefix `FATAL ERROR`.
- `6`**未正確的異常處理(Non-function Internal Exception Handler)** - There was an uncaught exception, but the internal fatal exception handler function was somehow set to a non-function, and could not be called.
- `7`**異常處理函數(shù)運(yùn)行時(shí)失敗(Internal Exception Handler Run-Time Failure)** - There was an uncaught exception, and the internal fatal exception handler function itself threw an error while attempting to handle it. This can happen, for example, if a `process.on('uncaughtException')` or `domain.on('error')` handler throws an error.
- `8` - 未使用(Unused). In previous versions of Node, exit code 8 sometimes indicated an uncaught exception.
- `9` - **無(wú)效的參數(shù)(Invalid Argument)** - Either an unknown option was specified, or an option requiring a value was provided without a value.
- `10`**運(yùn)行時(shí)失敗(Internal JavaScript Run-Time Failure)** - The JavaScript source code internal in Node's bootstrapping process threw an error when the bootstrapping function was called. This is extremely rare, and generally can only happen during development of Node itself.
- `12`**無(wú)效的調(diào)試參數(shù)(Invalid Debug Argument)** - The `--debug` and/or `--debug-brk` options were set, but an invalid port number was chosen.
- `>128`**信號(hào)退出(Signal Exits)** - If Node receives a fatal signal such as `SIGKILL` or `SIGHUP`, then its exit code will be `128` plus the value of the signal code. This is a standard Unix practice, since exit codes are defined to be 7-bit integers, and signal exits set the high-order bit, and then contain the value of the signal code.
### 事件: 'exit'
當(dāng)進(jìn)程將要退出時(shí)觸發(fā)。這是一個(gè)在固定時(shí)間檢查模塊狀態(tài)(如單元測(cè)試)的好時(shí)機(jī)。需要注意的是 'exit' 的回調(diào)結(jié)束后,主事件循環(huán)將不再運(yùn)行,所以計(jì)時(shí)器也會(huì)失效。
監(jiān)聽(tīng) `exit` 事件的例子:
~~~
process.on('exit', function() {
// 設(shè)置一個(gè)延遲執(zhí)行
setTimeout(function() {
console.log('主事件循環(huán)已停止,所以不會(huì)執(zhí)行');
}, 0);
console.log('退出前執(zhí)行');
});
~~~
### 事件: 'uncaughtException'(未捕獲錯(cuò)誤)
當(dāng)一個(gè)異常冒泡回歸到事件循環(huán)中就會(huì)觸發(fā)這個(gè)事件,如果建立了一個(gè)監(jiān)聽(tīng)器來(lái)監(jiān)聽(tīng)這個(gè)異常,默認(rèn)的行為(打印堆棧跟蹤信息并退出)就不會(huì)發(fā)生。
監(jiān)聽(tīng) `uncaughtException` 示例:
~~~
// 故意制造一個(gè)異常,而且不catch捕獲它.
nonexistentFunc();
console.log('This will not run.');
~~~
注意,`uncaughtException`未捕獲異常是一個(gè)非常粗略的異常處理。
盡量不要使用它,使用 [domains](#) 來(lái)代替它,如果你已經(jīng)使用了,請(qǐng)?jiān)诓惶幚磉@個(gè)異常之后重啟你的應(yīng)用。
請(qǐng) *不要* 象使用node.js的`有錯(cuò)誤回復(fù)執(zhí)行`這樣使用.一個(gè)未處理異常意味著你的應(yīng)用和你的擴(kuò)展Node.js自身是有未知狀態(tài)的。盲目的恢復(fù)意味著*任何事情*都可能發(fā)生。
你在升級(jí)的系統(tǒng)時(shí)拉掉了電源線,然后恢復(fù)了??赡?0次里有9次每一偶問(wèn)題,但是第10次,你的系統(tǒng)就會(huì)崩潰。
你已經(jīng)被警告。
### Signal Events
當(dāng)進(jìn)程接收到信號(hào)時(shí)觸發(fā)。信號(hào)列表詳見(jiàn) POSIX 標(biāo)準(zhǔn)的 sigaction(2)如 SIGINT、SIGUSR1 等。
監(jiān)聽(tīng) `SIGINT` 信號(hào)的示例:
~~~
// 設(shè)置 'SIGINT' 信號(hào)觸發(fā)事件
process.on('SIGINT', function() {
console.log('收到 SIGINT 信號(hào)。 退出請(qǐng)使用 Ctrl + D ');
});
~~~
在大多數(shù)終端下,一個(gè)發(fā)送 `SIGINT` 信號(hào)的簡(jiǎn)單方法是按下 `ctrl + c` 。
### process.stdout
一個(gè)指向`標(biāo)準(zhǔn)輸出流(stdout)`的 `可寫(xiě)的流(Writable Stream)`。
舉例: `console.log` 的實(shí)現(xiàn)
~~~
console.log = function(d) {
process.stdout.write(d + '\n');
};
~~~
process.stderr 和 process.stdout 不像 Node 中其他的流(Streams) 那樣,他們通常是阻塞式的寫(xiě)入。當(dāng)其引用指向 `普通文件` 或者 `TTY文件描述符` 時(shí)他們就是阻塞的(注:TTY 可以理解為終端的一種,可聯(lián)想 Pu
- 關(guān)于本文檔
- 概述
- 斷言 (assert)
- Buffer
- Addons插件
- 子進(jìn)程
- 集群
- 控制臺(tái)
- 加密(Crypto)
- 調(diào)試器
- DNS
- 域
- 事件 (Events)
- File System
- 全局對(duì)象
- HTTP
- HTTPS
- Modules
- net
- 操作系統(tǒng)
- 路徑 (Path)
- process
- punycode
- Query String
- Readline
- REPL
- Smalloc
- 流
- 字符串解碼器
- 定時(shí)器
- TLS (SSL)
- TTY
- UDP / 數(shù)據(jù)報(bào)套接字
- URL
- utils
- 執(zhí)行 JavaScript
- Zlib
- 進(jìn)度
- 感謝
