process對象用于處理與當前進程相關的事情,它是一個全局對象,可以在任何地方直接訪問到它而無需引入額外模塊。 它是 EventEmitter 的一個實例。
本章的示例可以從我的Github上下載到。
事件'exit'
當進程將要退出時觸發。這是一個在固定時間檢查模塊狀態(如單元測試)的好時機。需要注意的是 'exit' 的回調結束后,主事件循環將不再運行,所以計時器也會失效:
process.on('exit', function() { // 設置一個延遲執行 setTimeout(function() { console.log('主事件循環已停止,所以不會執行'); }, 0); console.log('退出前執行'); }); setTimeout(function() { console.log('1'); }, 500);
事件'uncaughtException'
捕獲那些咱沒有 try catch 的異常錯誤:
process.on('uncaughtException', function() { console.log('捕獲到一個異常'); }); var a = '123'; a.a(); //觸發異常事件 console.log('這句話撲街了,不會顯示出來');
但常規不建議使用該粗略的異常捕獲處理,建議使用 domains,可參考這篇文章。
事件'SIGINT'
捕獲當前進程接收到的信號(如按下了 ctrl + c):
process.on('SIGINT', function() { console.log('收到 SIGINT 信號。'); }); console.log('試著按下 ctrl + C'); setTimeout(function() { console.log('end'); }, 50000);
process.stdout
一個指向標準輸出流(stdout)的 可寫的流(Writable Stream):
process.stdout.write('這是一行數據\n這是第二行數據');
另可使用 process.stdout.isTTY 來判斷當前是否處于TTY上下文。
process.stderr
一個指向標準錯誤流(stderr)的 可寫的流(Writable Stream):
process.stderr.write('輸出一行標準錯誤流,效果跟stdout沒差');
process.stdin
一個指向 標準輸入流(stdin) 的可讀流(Readable Stream)。標準輸入流默認是暫停 (pause) 的,所以必須要調用 process.stdin.resume() 來恢復 (resume) 接收:
process.stdin.on('end', function() { process.stdout.write('end'); }); function gets(cb){ process.stdin.setEncoding('utf8'); //輸入進入流模式(flowing-mode,默認關閉,需用resume開啟),注意開啟后將無法read到數據 //見 https://github.com/nodejs/node-v0.x-archive/issues/5813 process.stdin.resume(); process.stdin.on('data', function(chunk) { console.log('start!'); //去掉下一行可一直監聽輸入,即保持標準輸入流為開啟模式 process.stdin.pause(); cb(chunk); }); console.log('試著在鍵盤敲幾個字然后按回車吧'); } gets(function(reuslt){ console.log("["+reuslt+"]"); //process.stdin.emit('end'); //觸發end事件 });
process.argv
返回當前命令行指令參數 ,但不包括node特殊(node-specific) 的命令行選項(參數)。常規第一個元素會是 'node', 第二個元素將是 .Js 文件的名稱。接下來的元素依次是命令行傳入的參數:
//試著執行 $node --harmony argv.js a b console.log(process.argv); //[ 'node', 'E:\\github\\nodeAPI\\process\\argv.js', 'a', 'b' ] process.argv.forEach(function(val, index, array) { console.log(index + ': ' + val); });
process.execArgv
與 process.argv 類似,不過是用于返回 node特殊(node-specific) 的命令行選項(參數)。另外所有文件名之后的參數都會被忽視:
//試著執行 $node --harmony execArgv a b --version console.log(process.execArgv); //[ '--harmony' ] process.execArgv.forEach(function(val, index, array) { console.log(index + ': ' + val); });
process.abort()
觸發node的abort事件,退出當前進程:
process.abort();
console.log('在輸出這句話之前就退出了');
process.execPath
獲取當前進程的這個可執行文件的絕對路徑:
console.log(process.execPath); //C:\Program Files\nodejs\node.exe
process.cwd
返回當前進程的工作目錄:
console.log('當前目錄:' + process.cwd()); //當前目錄:E:\github\nodeAPI\process
process.chdir(directory)
改變進程的當前進程的工作目錄(該目錄必須已存在),若操作失敗則拋出異常:
var path = require('path'); console.log('當前目錄:' + process.cwd()); //當前目錄:E:\github\nodeAPI\process try { process.chdir(path.resolve('.','tmp')); console.log('新目錄:' + process.cwd()); //新目錄:E:\github\nodeAPI\process\tmp } catch (err) { console.log('chdir: ' + err); }
process.env
獲取當前系統環境信息的對象,常規可以用來進一步獲取環境變量、用戶名等系統信息:
console.log(process.env); console.log('username: ' + process.env.USERNAME); console.log('PATH: ' + process.env.PATH);
process.exit([code])
終止當前進程并返回給定的 code。如果省略了 code,退出是會默認返回成功的狀態碼('success' code) 也就是 0:
process.exit(1); //node的shell將捕獲到值為1的返回碼
更多的返回狀態碼可參考下方列表:
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 解析錯誤(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 評估失敗(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 致命錯誤(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 異常處理函數運行時失敗(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 - 無效的參數(Invalid Argument) - Either an unknown option was specified, or an option requiring a value was provided without a value.
10 運行時失敗(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 無效的調試參數(Invalid Debug Argument) - The --debug and/or --debug-brk options were set, but an invalid port number was chosen.
>128 信號退出(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.
process.exitCode
可以自定義退出進程時node shell捕獲到的狀態碼(必須是正常結束進程或者使用process.exit()指令退出)
process.exitCode = 4;
process.exit();
如果指名了 process.exit(code) 中退出的錯誤碼 (code),則會覆蓋掉 process.exitCode 的設置。
process.version
一個暴露編譯時存儲版本信息的內置變量 NODE_VERSION 的屬性:
console.log('版本: ' + process.version); //版本: v0.12.7
process.versions
一個暴露存儲 node 以及其依賴包 版本信息的屬性:
console.log(process.versions); //{ http_parser: '2.3', // node: '0.12.7', // v8: '3.28.71.19', // uv: '1.6.1', // zlib: '1.2.8', // modules: '14', // openssl: '1.0.1p' }
process.config
一個包含用來編譯當前 node.exe 的配置選項的對象:
console.log(process.config); //{ target_defaults: //{ cflags: [], // default_configuration: 'Release', // defines: [], // include_dirs: [], // libraries: [] }, // variables: // { clang: 0, // host_arch: 'x64', // icu_data_file: 'icudt54l.dat', // icu_data_in: '../../deps/icu/source/data/in\\icudt54l.dat', // icu_endianness: 'l', // icu_gyp_path: 'tools/icu/icu-generic.gyp', // icu_locales: 'en,root', // icu_path: 'deps\\icu', // icu_small: true, // icu_ver_major: '54', // node_install_npm: true, // node_prefix: '', // node_shared_cares: false, // node_shared_http_parser: false, // node_shared_libuv: false, // node_shared_openssl: false, // node_shared_v8: false, // node_shared_zlib: false, // node_tag: '', // node_use_dtrace: false, // node_use_etw: true, // node_use_mdb: false, // node_use_openssl: true, // node_use_perfctr: true, // openssl_no_asm: 0, // python: 'C:\\Python27\\python.exe', // target_arch: 'x64', // uv_library: 'static_library', // v8_enable_gdbjit: 0, // v8_enable_i18n_support: 1, // v8_no_strict_aliasing: 1, // v8_optimized_debug: 0, // v8_random_seed: 0, // v8_use_snapshot: false, // visibility: '', // want_separate_host_toolset: 0 } }
process.pid
獲得當前進程的pid:
console.log(process.pid);
process.kill(pid, [signal])
結束對應某pid的進程并發送一個信號(若沒定義信號值則默認為'SIGTERM'):
process.kill(process.pid, 'SIGTERM');
process.title
獲取或設置當前進程的標題名稱:
console.log(process.title); // 管理員: E:\Program Files\WebStorm 9.0.1\lib\libpty\win\x86\winpty-agent.exe - node title process.title = 'new title!!!'; console.log(process.title); //new title!!!
process.arch
返回當前CPU的架構('arm'、'ia32' 或者 'x64'):
console.log(process.arch); //x64
process.platform
返回當前平臺類型('darwin', 'freebsd', 'linux', 'sunos' 或者 'win32'):
console.log(process.platform); //win32
process.memoryUsage()
返回一個對象,它描述了Node進程的內存使用情況,其單位是bytes:
console.log(process.memoryUsage()); //{ rss: 16875520, heapTotal: 9751808, heapUsed: 3997040 }
process.nextTick(callback)
算是 process 對象最重要的一個屬性方法了,表示在事件循環(EventLoop)的下一次循環中調用 callback 回調函數。
要注意的是它總會在I/O操作(比如查詢數據)之前先執行。
示例:
console.log('開始'); process.nextTick(function() { console.log('nextTick 回調'); }); setTimeout(function(){ console.log('新的EventLoop!') }, 2000); console.log('當前EventLoop'); // 輸出: // 當前EventLoop // nextTick 回調 // 新的EventLoop!
很多情況下都容易把它和 setImmediate 概念混淆,可以在這里看下它們的區別。
process.umask([mask])
設置或者讀取進程的文件模式的創建掩碼(屏蔽字)。子進程從父進程中繼承這個掩碼。如果設定了參數 mask 那么返回舊的掩碼,否則返回當前的掩碼:
var newmask = 77, oldmask = process.umask(newmask); console.log('原掩碼: ' + oldmask.toString(8) + '\n' + '新掩碼: ' + newmask.toString(8)); //原掩碼: 0 //新掩碼: 115
關于 umask 的更多信息可參考維基百科。
process.uptime()
返回 Node 程序已運行的秒數:
console.log('初始時間是:' + process.uptime()); var arr = new Array(10000000); var s = arr.join(','); console.log('處理數組后的時間是:' + process.uptime()); //初始時間是:0.436 //處理數組后的時間是:1.068
process.hrtime()
返回當前的高分辨時間,形式為 [秒,納秒] 的元組數組。它是相對于在過去的任意時間。該值與日期無關,因此不受時鐘漂移的影響。主要用途是可以通過精確的時間間隔,來衡量程序的性能。示例:
var t1 = process.hrtime(); var arr = new Array(40000000), s = arr.join(','); var t2 = process.hrtime(); console.log('處理數組共花費了%d秒,詳細為%d納秒', (t2[0] - t1[0]), (t2[1] - t1[1])); //處理數組共花費了2秒,詳細為227005133納秒
下面介紹的幾個方法均為*nix系統(POSIX標準的系統平臺)下獨有的,win下不存在。
process.getgid()
獲取進程的群組標識。獲取到的是群組的數字ID,不是群組名稱。
if (process.getgid) { console.log('當前 gid: ' + process.getgid()); }
process.setgid(id)
設置進程的群組標識。參數可以是一個數字ID或者群組名字符串。如果指定了一個群組名,這個方法會阻塞等待將群組名解析為數字ID。
if (process.getgid && process.setgid) { console.log('當前 gid: ' + process.getgid()); try { process.setgid(501); console.log('新 gid: ' + process.getgid()); } catch (err) { console.log('設置 gid 失敗: ' + err); } }
process.getuid()
獲取執行進程的用戶ID(詳見getgid(2))。這是用戶的數字ID,不是用戶名。
if (process.getuid) { console.log('當前 uid: ' + process.getuid()); }
process.setuid(id)
設置執行進程的用戶ID。參數可以使一個數字ID或者用戶名字符串。如果指定了一個用戶名,那么該方法會阻塞等待將用戶名解析為數字ID。
if (process.getuid && process.setuid) { console.log('當前 uid: ' + process.getuid()); try { process.setuid(501); console.log('新 uid: ' + process.getuid()); } catch (err) { console.log('設置 uid 失敗: ' + err); } }
process.getgroups()
返回一個保存補充組ID(supplementary group ID)的數組。
if (process.getgroups) { console.log('supplementary group ID: \n' ); process.getgroups().forEach(function(g){ console.log(g) }) }
process.setgroups(groups)
設置補充分組(supplementary group)的ID標識. 這是一個特殊的操作, 意味著你必須擁有root或者CAP_SETGID權限才可以。
if(process.setgroups){ gid = [ 27, 30, 46, 1000 ]; process.setgroups(gid); console.log(process.getgroups()); }
process.initgroups(user, extra_group)
讀取 /etc/group 并且初始化group分組訪問列表,使用改成員所在的所有分組, 這是一個特殊的操作, 意味著你必須擁有root或者CAP_SETGID權限才可以。
參數 user 是一個用戶名或者用戶ID,而 extra_group 是分組的組名或者分組ID。
console.log(process.getgroups()); // [ 0 ] process.initgroups('bnoordhuis', 1000); // switch user console.log(process.getgroups()); // [ 27, 30, 46, 1000, 0 ] process.setgid(1000); // drop root gid console.log(process.getgroups()); // [ 27, 30, 46, 1000 ]
process的API就介紹到這里,共勉~
文章列表