{"id":324,"date":"2017-12-05T10:01:55","date_gmt":"2017-12-05T02:01:55","guid":{"rendered":"https:\/\/blog.lcsoul.cn\/?p=324"},"modified":"2017-12-05T10:01:55","modified_gmt":"2017-12-05T02:01:55","slug":"child-process%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/blog.lcsoul.top\/index.php\/2017\/12\/05\/child-process%e6%a8%a1%e5%9d%97\/","title":{"rendered":"Child Process\u6a21\u5757"},"content":{"rendered":"<p><!--more--><\/p>\n<h2>\u76ee\u5f55<\/h2>\n<ul>\n<li>exec()<\/li>\n<li>execSync()<\/li>\n<li>execFile()<\/li>\n<li>spawn()<\/li>\n<li>fork()<\/li>\n<li>send()<\/li>\n<\/ul>\n<p>child_process\u6a21\u5757\u7528\u4e8e\u65b0\u5efa\u5b50\u8fdb\u7a0b\u3002\u5b50\u8fdb\u7a0b\u7684\u8fd0\u884c\u7ed3\u679c\u50a8\u5b58\u5728\u7cfb\u7edf\u7f13\u5b58\u4e4b\u4e2d\uff08\u6700\u5927200KB\uff09\uff0c\u7b49\u5230\u5b50\u8fdb\u7a0b\u8fd0\u884c\u7ed3\u675f\u4ee5\u540e\uff0c\u4e3b\u8fdb\u7a0b\u518d\u7528\u56de\u8c03\u51fd\u6570\u8bfb\u53d6\u5b50\u8fdb\u7a0b\u7684\u8fd0\u884c\u7ed3\u679c\u3002<\/p>\n<h2>1.exec()<\/h2>\n<p><code>exec<\/code>\u65b9\u6cd5\u7528\u4e8e\u6267\u884cbash\u547d\u4ee4\uff0c\u5b83\u7684\u53c2\u6570\u662f\u4e00\u4e2a\u547d\u4ee4\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"prettyprint linenums\">var exec = require('child_process').exec;\nvar ls = exec('ls -l', function (error, stdout, stderr) {\n  if (error) {\n    console.log(error.stack);\n    console.log('Error code: ' + error.code);\n  }\n  console.log('Child Process STDOUT: ' + stdout);\n});\n<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u7684<code>exec<\/code>\u65b9\u6cd5\u7528\u4e8e\u65b0\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u7f13\u5b58\u5b83\u7684\u8fd0\u884c\u7ed3\u679c\uff0c\u8fd0\u884c\u7ed3\u675f\u540e\u8c03\u7528\u56de\u8c03\u51fd\u6570\u3002<br \/>\n<code>exec<\/code>\u65b9\u6cd5\u6700\u591a\u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6240\u8981\u6267\u884c\u7684shell\u547d\u4ee4\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u56de\u8c03\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e09\u4e2a\u53c2\u6570\uff0c\u5206\u522b\u662f\u53d1\u751f\u7684\u9519\u8bef\u3001\u6807\u51c6\u8f93\u51fa\u7684\u663e\u793a\u7ed3\u679c\u3001\u6807\u51c6\u9519\u8bef\u7684\u663e\u793a\u7ed3\u679c\u3002<br \/>\n\u7531\u4e8e\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u90fd\u662f\u6d41\u5bf9\u8c61\uff08stream\uff09\uff0c\u53ef\u4ee5\u76d1\u542cdata\u4e8b\u4ef6\uff0c\u56e0\u6b64\u4e0a\u9762\u7684\u4ee3\u7801\u4e5f\u53ef\u4ee5\u5199\u6210\u4e0b\u9762\u8fd9\u6837\u3002<\/p>\n<pre class=\"prettyprint linenums\">var exec = require('child_process').exec;\nvar child = exec('ls -l');\nchild.stdout.on('data', function(data) {\n  console.log('stdout: ' + data);\n});\nchild.stderr.on('data', function(data) {\n  console.log('stdout: ' + data);\n});\nchild.on('close', function(code) {\n  console.log('closing code: ' + code);\n});\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u8fd8\u8868\u660e\uff0c\u5b50\u8fdb\u7a0b\u672c\u8eab\u6709<code>close<\/code>\u4e8b\u4ef6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570\u3002<br \/>\n\u4e0a\u9762\u7684\u4ee3\u7801\u8fd8\u6709\u4e00\u4e2a\u597d\u5904\u3002\u76d1\u542cdata\u4e8b\u4ef6\u4ee5\u540e\uff0c\u53ef\u4ee5\u5b9e\u65f6\u8f93\u51fa\u7ed3\u679c\uff0c\u5426\u5219\u53ea\u6709\u7b49\u5230\u5b50\u8fdb\u7a0b\u7ed3\u675f\uff0c\u624d\u4f1a\u8f93\u51fa\u7ed3\u679c\u3002\u6240\u4ee5\uff0c\u5982\u679c\u5b50\u8fdb\u7a0b\u8fd0\u884c\u65f6\u95f4\u8f83\u957f\uff0c\u6216\u8005\u662f\u6301\u7eed\u8fd0\u884c\uff0c\u7b2c\u4e8c\u79cd\u5199\u6cd5\u66f4\u597d\u3002<br \/>\n\u4e0b\u9762\u662f\u53e6\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5047\u5b9a\u6709\u4e00\u4e2achild.js\u6587\u4ef6\u3002<\/p>\n<pre class=\"prettyprint linenums\">\/\/ child.js\nvar exec = require('child_process').exec;\nexec('node -v', function(error, stdout, stderr) {\n  console.log('stdout: ' + stdout);\n  console.log('stderr: ' + stderr);\n  if (error !== null) {\n    console.log('exec error: ' + error);\n  }\n});\n<\/pre>\n<p>\u8fd0\u884c\u540e\uff0c\u8be5\u6587\u4ef6\u7684\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\u3002<\/p>\n<pre class=\"prettyprint linenums\">$ node child.js\nstdout: v0.11.14\nstderr:\n<\/pre>\n<p><code>exec<\/code>\u65b9\u6cd5\u4f1a\u76f4\u63a5\u8c03\u7528bash\uff08<code>\/bin\/sh<\/code>\u7a0b\u5e8f\uff09\u6765\u89e3\u91ca\u547d\u4ee4\uff0c\u6240\u4ee5\u5982\u679c\u6709\u7528\u6237\u8f93\u5165\u7684\u53c2\u6570\uff0c<code>exec<\/code>\u65b9\u6cd5\u662f\u4e0d\u5b89\u5168\u7684\u3002<\/p>\n<pre class=\"prettyprint linenums\">var path = \";user input\";\nchild_process.exec('ls -l ' + path, function (err, data) {\n  console.log(data);\n});\n<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u8868\u793a\uff0c\u5728bash\u73af\u5883\u4e0b\uff0cls -l; user input\u4f1a\u76f4\u63a5\u8fd0\u884c\u3002\u5982\u679c\u7528\u6237\u8f93\u5165\u6076\u610f\u4ee3\u7801\uff0c\u5c06\u4f1a\u5e26\u6765\u5b89\u5168\u98ce\u9669\u3002\u56e0\u6b64\uff0c\u5728\u6709\u7528\u6237\u8f93\u5165\u7684\u60c5\u51b5\u4e0b\uff0c\u6700\u597d\u4e0d\u4f7f\u7528exec\u65b9\u6cd5\uff0c\u800c\u662f\u4f7f\u7528execFile\u65b9\u6cd5\u3002<\/p>\n<h2>2.execSync()<\/h2>\n<p><code>execSync<\/code>\u662f<code>exec<\/code>\u7684\u540c\u6b65\u6267\u884c\u7248\u672c\u3002<br \/>\n\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6240\u8981\u6267\u884c\u7684\u547d\u4ee4\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7528\u6765\u914d\u7f6e\u6267\u884c\u73af\u5883\u3002<\/p>\n<pre class=\"prettyprint linenums\">var execSync = require(\"child_process\").execSync;\nvar SEPARATOR = process.platform === 'win32' ? ';' : ':';\nvar env = Object.assign({}, process.env);\nenv.PATH = path.resolve('.\/node_modules\/.bin') + SEPARATOR + env.PATH;\nfunction myExecSync(cmd) {\n  var output = execSync(cmd, {\n    cwd: process.cwd(),\n    env: env\n  });\n  console.log(output);\n}\nmyExecSync('eslint .');\n<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<code>execSync<\/code>\u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002\u8be5\u5bf9\u8c61\u7684<code>cwd<\/code>\u5c5e\u6027\u6307\u5b9a\u811a\u672c\u7684\u5f53\u524d\u76ee\u5f55\uff0c<code>env<\/code>\u5c5e\u6027\u6307\u5b9a\u73af\u5883\u53d8\u91cf\u3002\u4e0a\u9762\u4ee3\u7801\u5c06<code>.\/node_modules\/.bin<\/code>\u76ee\u5f55\uff0c\u5b58\u5165$PATH\u53d8\u91cf\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e0d\u52a0\u8def\u5f84\uff0c\u5f15\u7528\u9879\u76ee\u5185\u90e8\u7684\u6a21\u5757\u547d\u4ee4\u4e86\uff0c\u6bd4\u5982<code>eslint<\/code>\u547d\u4ee4\u5b9e\u9645\u6267\u884c\u7684\u662f<code>.\/node_modules\/.bin\/eslint<\/code>\u3002<\/p>\n<h2>3.execFile()<\/h2>\n<p><code>execFile<\/code>\u65b9\u6cd5\u76f4\u63a5\u6267\u884c\u7279\u5b9a\u7684\u7a0b\u5e8f\uff0c\u53c2\u6570\u4f5c\u4e3a\u6570\u7ec4\u4f20\u5165\uff0c\u4e0d\u4f1a\u88abbash\u89e3\u91ca\uff0c\u56e0\u6b64\u5177\u6709\u8f83\u9ad8\u7684\u5b89\u5168\u6027\u3002<\/p>\n<pre class=\"prettyprint linenums\">var child_process = require('child_process');\nvar path = \".\";\nchild_process.execFile('\/bin\/ls', ['-l', path], function (err, result) {\n    console.log(result)\n});\n<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5047\u5b9a<code>path<\/code>\u6765\u81ea\u7528\u6237\u8f93\u5165\uff0c\u5982\u679c\u5176\u4e2d\u5305\u542b\u4e86\u5206\u53f7\u6216\u53cd\u5f15\u53f7\uff0cls\u7a0b\u5e8f\u4e0d\u7406\u89e3\u5b83\u4eec\u7684\u542b\u4e49\uff0c\u56e0\u6b64\u4e5f\u5c31\u5f97\u4e0d\u5230\u8fd0\u884c\u7ed3\u679c\uff0c\u5b89\u5168\u6027\u5c31\u5f97\u5230\u4e86\u63d0\u9ad8\u3002<\/p>\n<h2>4.spawn()<\/h2>\n<p><code>spawn<\/code>\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6765\u6267\u884c\u7279\u5b9a\u547d\u4ee4\uff0c\u7528\u6cd5\u4e0e<code>execFile<\/code>\u65b9\u6cd5\u7c7b\u4f3c\uff0c\u4f46\u662f\u6ca1\u6709\u56de\u8c03\u51fd\u6570\uff0c\u53ea\u80fd\u901a\u8fc7\u76d1\u542c\u4e8b\u4ef6\uff0c\u6765\u83b7\u53d6\u8fd0\u884c\u7ed3\u679c\u3002\u5b83\u5c5e\u4e8e\u5f02\u6b65\u6267\u884c\uff0c\u9002\u7528\u4e8e\u5b50\u8fdb\u7a0b\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u60c5\u51b5\u3002<\/p>\n<pre class=\"prettyprint linenums\">var child_process = require('child_process');\nvar path = '.';\nvar ls = child_process.spawn('\/bin\/ls', ['-l', path]);\nls.stdout.on('data', function (data) {\n  console.log('stdout: ' + data);\n});\nls.stderr.on('data', function (data) {\n  console.log('stderr: ' + data);\n});\nls.on('close', function (code) {\n  console.log('child process exited with code ' + code);\n});\n<\/pre>\n<p><code>spawn<\/code>\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u662f\u53ef\u6267\u884c\u6587\u4ef6\uff0c\u7b2c\u4e8c\u4e2a\u662f\u53c2\u6570\u6570\u7ec4\u3002<br \/>\n<code>spawn<\/code>\u5bf9\u8c61\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4ee3\u8868\u5b50\u8fdb\u7a0b\u3002\u8be5\u5bf9\u8c61\u90e8\u7f72\u4e86<code>EventEmitter<\/code>\u63a5\u53e3\uff0c\u5b83\u7684<code>data<\/code>\u4e8b\u4ef6\u53ef\u4ee5\u76d1\u542c\uff0c\u4ece\u800c\u5f97\u5230\u5b50\u8fdb\u7a0b\u7684\u8f93\u51fa\u7ed3\u679c\u3002<br \/>\n<code>spawn<\/code>\u65b9\u6cd5\u4e0e<code>exec<\/code>\u65b9\u6cd5\u975e\u5e38\u7c7b\u4f3c\uff0c\u53ea\u662f\u4f7f\u7528\u683c\u5f0f\u7565\u6709\u533a\u522b\u3002<\/p>\n<pre class=\"prettyprint linenums\">child_process.exec(command, [options], callback)\nchild_process.spawn(command, [args], [options])\n<\/pre>\n<h2>5.fork()<\/h2>\n<p><code>fork<\/code>\u65b9\u6cd5\u76f4\u63a5\u521b\u5efa\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u6267\u884cNode\u811a\u672c\uff0c<code>fork('.\/child.js')<\/code> \u76f8\u5f53\u4e8e spawn(&#8216;node&#8217;, [&#8216;.\/child.js&#8217;]) \u3002\u4e0e<code>spawn<\/code>\u65b9\u6cd5\u4e0d\u540c\u7684\u662f\uff0c<code>fork<\/code>\u4f1a\u5728\u7236\u8fdb\u7a0b\u4e0e\u5b50\u8fdb\u7a0b\u4e4b\u95f4\uff0c\u5efa\u7acb\u4e00\u4e2a\u901a\u4fe1\u7ba1\u9053\uff0c\u7528\u4e8e\u8fdb\u7a0b\u4e4b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n<pre class=\"prettyprint linenums\">var n = child_process.fork('.\/child.js');\nn.on('message', function(m) {\n  console.log('PARENT got message:', m);\n});\nn.send({ hello: 'world' });\n<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c<code>fork<\/code>\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u4ee3\u8868\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7ba1\u9053\u7684\u5bf9\u8c61\uff0c\u5bf9\u8be5\u5bf9\u8c61\u53ef\u4ee5\u76d1\u542c<code>message<\/code>\u4e8b\u4ef6\uff0c\u7528\u6765\u83b7\u53d6\u5b50\u8fdb\u7a0b\u8fd4\u56de\u7684\u4fe1\u606f\uff0c\u4e5f\u53ef\u4ee5\u5411\u5b50\u8fdb\u7a0b\u53d1\u9001\u4fe1\u606f\u3002<br \/>\nchild.js\u811a\u672c\u7684\u5185\u5bb9\u5982\u4e0b\u3002<\/p>\n<pre class=\"prettyprint linenums\">process.on('message', function(m) {\n  console.log('CHILD got message:', m);\n});\nprocess.send({ foo: 'bar' });\n<\/pre>\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u5b50\u8fdb\u7a0b\u76d1\u542c<code>message<\/code>\u4e8b\u4ef6\uff0c\u5e76\u5411\u7236\u8fdb\u7a0b\u53d1\u9001\u4fe1\u606f\u3002<\/p>\n<h2>6.send()<\/h2>\n<p>\u4f7f\u7528 <code>child_process.fork() <\/code>\u751f\u6210\u65b0\u8fdb\u7a0b\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u7528 <code>child.send(message, [sendHandle])<\/code> \u5411\u65b0\u8fdb\u7a0b\u53d1\u9001\u6d88\u606f\u3002\u65b0\u8fdb\u7a0b\u4e2d\u901a\u8fc7\u76d1\u542c<code>message<\/code>\u4e8b\u4ef6\uff0c\u6765\u83b7\u53d6\u6d88\u606f\u3002<br \/>\n\u4e0b\u9762\u7684\u4f8b\u5b50\u662f\u4e3b\u8fdb\u7a0b\u7684\u4ee3\u7801\u3002<\/p>\n<pre class=\"prettyprint linenums\">var cp = require('child_process');\nvar n = cp.fork(__dirname + '\/sub.js');\nn.on('message', function(m) {\n  console.log('PARENT got message:', m);\n});\nn.send({ hello: 'world' });\n<\/pre>\n<p>\u4e0b\u9762\u662f\u5b50\u8fdb\u7a0bsub.js\u4ee3\u7801\u3002<\/p>\n<pre class=\"prettyprint linenums\">process.on('message', function(m) {\n  console.log('CHILD got message:', m);\n});\nprocess.send({ foo: 'bar' });\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[30,34,35,36,40,67,76],"class_list":["post-324","post","type-post","status-publish","format-standard","hentry","category-nodejs","tag-child-process","tag-exec","tag-execfile","tag-execsync","tag-fork","tag-send","tag-spawn"],"_links":{"self":[{"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/posts\/324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/comments?post=324"}],"version-history":[{"count":0,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lcsoul.top\/index.php\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}