{"id":5801,"date":"2017-06-28T19:20:20","date_gmt":"2017-06-28T11:20:20","guid":{"rendered":"https:\/\/sdeno.com\/?p=5801"},"modified":"2019-07-02T18:54:44","modified_gmt":"2019-07-02T10:54:44","slug":"node-js%e6%89%a7%e8%a1%8clinux-shell%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5801","title":{"rendered":"node.js\u6267\u884clinux shell\u4ee3\u7801"},"content":{"rendered":"<p>node shell<\/p>\n<p>node.js \u5b50\u8fdb\u7a0b<\/p>\n<pre>ls -a \/\/\u5217\u51fa\u6240\u6709\u6587\u4ef6\u53ca\u6587\u4ef6\u5939\uff0c\u5e76\u663e\u793a\u6240\u6709\u9690\u85cf\u6587\u4ef6<\/pre>\n<p>\u5f02\u6b65\uff1a\uff08\u5e38\u7528\uff09<br \/>\nspawn,exec,execFile,fork<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">1\uff0cspawn<\/span><\/p>\n<pre>var child = require('child_process');\r\nvar du = child.spawn('ls', ['-a']);\r\n\r\ndu.stdout.on('data', function (data) {\r\n   console.log('\u8f93\u51fa\u5185\u5bb9\u662f ' + data); \/\/\u8f93\u51fa\u7684\u5185\u5bb9\u548c\u76f4\u63a5\u6267\u884cls -a\u7684\u6548\u679c\u4e00\u6837\r\n});\r\n\r\ndu.stderr.on('data', function (data) {\r\n   console.log('stderr: ' + data);\r\n});\r\n\r\ndu.on('exit', function (code) {\r\n   console.log('child process exited with code ' + code);\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">2\uff0cexec<\/span><\/p>\n<p>\/\/\u63a8\u8350\uff0c\u53ef\u76f4\u63a5\u4e00\u53e5\u8bdd\u6267\u884c\uff0c\u5e76\u4e14\u6709\u56de\u8c03<\/p>\n<pre>var childProcess = require('child_process');\r\nchildProcess.exec('ls -a', function (error, stdout, stderr) {\r\n if (error) {\r\n   console.log(error.stack);\r\n   console.log('Error code: '+error.code);\r\n }\r\n   console.log('\u8f93\u51fa\u5185\u5bb9\u662f: '+stdout);\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">3\uff0cexecFile<\/span><\/p>\n<p>\/\/\u52a0\u8f7d\u5e76\u6267\u884c\u547d\u4ee4\u6587\u4ef6\uff0c\u53c2\u6570\u9700\u8981\u6570\u7ec4\u5f62\u5f0f<\/p>\n<pre>var childProcess = require('child_process');\r\nvar path = \".\";\r\nchildProcess.execFile('\/bin\/ls', ['-l', path], function (err, result) {\r\n  console.log(result)\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">4\uff0cfork<\/span><\/p>\n<p>\/\/\u8fdb\u7a0b\u4e4b\u95f4\u4f20\u53c2<\/p>\n<pre>\/\/pather.js\r\nvar childProcess = require('child_process');\r\nvar n = childProcess.fork('.\/son.js'); \/\/\u5f15\u5165\u8981\u76d1\u542c\u7684\u6a21\u5757\r\n\r\nn.on('message', function(m) {\r\n  console.log('\u513f\u5b50\u53d1\u6765\u7684\u4fe1\u606f: ', m); \/\/\u6b64\u5904\u662f\uff0c\u8001\u7238\u63a5\u53d7\u5230\u513f\u5b50\u53d1\u6765\u7684\u4fe1\u606f\r\n});\r\nn.send({ hello: '\u513f\u5b50\u6211\u662f\u8001\u7238' }); \/\/\u6b64\u5904\u662f\uff0c\u8001\u7238\u7ed9\u513f\u5b50\u53d1\u9001\u7684\u4fe1\u606f\r\n\r\n\r\n\r\n\/\/son.js\r\nprocess.on('message', function(m) {\r\n console.log('\u8001\u7238\u53d1\u6765\u7684\u4fe1\u606f:', m); \/\/\u6536\u5230\u3002\u8001\u7238\u53d1\u6765\u7684\u4fe1\u606f\r\n});\r\nprocess.send({ Hello: '\u8001\u7238\u6211\u662f\u513f\u5b50' }); \/\/\u5411\u8001\u7238\u53d1\u9001\u4fe1\u606f\r\nprocess.exit(0);  \/\/\u513f\u5b50\u53d1\u6570\u636e\u7ed9\u8001\u7238\u540e\uff0c\u4efb\u52a1\u5b8c\u6210\u9000\u51fa\u3002<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u540c\u6b65\uff1a<br \/>\nspawnSync,execFileSync,execSync<\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n","protected":false},"excerpt":{"rendered":"<p>node shell node.js \u5b50\u8fdb\u7a0b ls -a \/\/\u5217\u51fa\u6240\u6709\u6587\u4ef6\u53ca\u6587\u4ef6\u5939\uff0c\u5e76\u663e\u793a\u6240\u6709\u9690\u85cf\u6587\u4ef6 \u5f02\u6b65\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-5801","post","type-post","status-publish","format-standard","hentry","category-nodejs"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5801","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5801"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5801\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}