dis*_*ted 162 javascript command sync exec node.js
我需要在node.js函数中
result = execSync('node -v');
Run Code Online (Sandbox Code Playgroud)
将同步执行给定的命令行并返回该命令文本的所有stdout.
PS.同步是错误的.我知道.仅供个人使用.
UPDATE
现在我们有mgutz的解决方案,它给我们退出代码,但不是stdout!仍在等待更精确的答案.
UPDATE
mgutz更新了他的答案,解决方案在这里:)
另外,正如dgo.a所提到的,有独立模块exec-sync
更新2014-07-30
ShellJS lib到了.认为这是目前最好的选择.
更新2015-02-10
最后!NodeJS 0.12原生支持execSync
.
见官方文档
Ben*_*aum 133
Node.js(从版本0.12开始 - 所以有一段时间)支持execSync
:
child_process.execSync(command[, options])
Run Code Online (Sandbox Code Playgroud)
您现在可以直接执行此操作:
const execSync = require('child_process').execSync;
code = execSync('node -v');
Run Code Online (Sandbox Code Playgroud)
它会做你期望的.(默认为将i/o结果传递给父进程).请注意,您spawnSync
现在也可以.
mgu*_*utz 54
请参阅execSync库.
使用node-ffi相当容易.我不建议使用服务器进程,但对于一般开发实用程序,它可以完成任务.安装库.
npm install node-ffi
Run Code Online (Sandbox Code Playgroud)
示例脚本:
var FFI = require("node-ffi");
var libc = new FFI.Library(null, {
"system": ["int32", ["string"]]
});
var run = libc.system;
run("echo $USER");
Run Code Online (Sandbox Code Playgroud)
[编辑2012年6月:如何获得STDOUT]
var lib = ffi.Library(null, {
// FILE* popen(char* cmd, char* mode);
popen: ['pointer', ['string', 'string']],
// void pclose(FILE* fp);
pclose: ['void', [ 'pointer']],
// char* fgets(char* buff, int buff, in)
fgets: ['string', ['string', 'int','pointer']]
});
function execSync(cmd) {
var
buffer = new Buffer(1024),
result = "",
fp = lib.popen(cmd, 'r');
if (!fp) throw new Error('execSync error: '+cmd);
while(lib.fgets(buffer, 1024, fp)) {
result += buffer.readCString();
};
lib.pclose(fp);
return result;
}
console.log(execSync('echo $HOME'));
Run Code Online (Sandbox Code Playgroud)
fal*_*lko 31
使用ShellJS模块.
exec函数没有提供回调.
例:
var version = exec('node -v').output;
Run Code Online (Sandbox Code Playgroud)
nab*_*nab 23
在node.js中有一个很好的流控制模块叫做asyncblock.如果在函数中包装代码对于您的情况是可行的,则可以考虑以下示例:
var asyncblock = require('asyncblock');
var exec = require('child_process').exec;
asyncblock(function (flow) {
exec('node -v', flow.add());
result = flow.wait();
console.log(result); // There'll be trailing \n in the output
// Some other jobs
console.log('More results like if it were sync...');
});
Run Code Online (Sandbox Code Playgroud)
Ivo*_*zel 10
这是不可能的Node.js,都child_process.spawn
和child_process.exec
从地上爬起来是异步建成.
有关详细信息,请参阅:https://github.com/ry/node/blob/master/lib/child_process.js
如果你真的想要这个阻塞,那么把所有需要发生的事情放在回调中,或者建立你自己的队列以阻塞的方式处理它,我想你可以使用Async.js来完成这个任务.
或者,如果你有太多的时间花费,那么在Node.js中自我攻击.
这是我找到的最简单的方法:
exec-Sync:https : //github.com/jeremyfa/node-exec-sync
(不要与execSync混淆.)
同步执行shell命令.将其用于迁移脚本,cli程序,但不用于常规服务器代码.例:
var execSync = require('exec-sync');
var user = execSync('echo $USER');
console.log(user);
Run Code Online (Sandbox Code Playgroud)
只是补充说,即使你应该使用它们的用例很少,spawnSync
/ execFileSync
/ execSync
也被添加到这些提交中的node.js:https://github.com/joyent/node/compare/d58c206862dc...e8df2676748e
原生 Node.js 解决方案是:
const {execSync} = require('child_process');
const result = execSync('node -v'); // this do the trick
Run Code Online (Sandbox Code Playgroud)
请注意,某些命令返回 Buffer
而不是string
. 如果您string
只需要添加encoding
到 execSync 选项:
const result = execSync('git rev-parse HEAD', {encoding: 'utf8'});
Run Code Online (Sandbox Code Playgroud)
...并且同步执行超时也很好:
const result = execSync('git rev-parse HEAD', {encoding: 'utf8', timeout: 10000});
Run Code Online (Sandbox Code Playgroud)
5年来我的方法是有2条线;
const { execSync } = require('child_process');
const shell = (cmd) => execSync(cmd, {encoding: 'utf8'});
Run Code Online (Sandbox Code Playgroud)
然后享受:
shell('git remote -v')
或者
out = shell('ls -l')
..等等
小智 5
你可以使用纤维实现这一目标.例如,使用我的公共节点库,代码如下所示:
result = require('subprocess').command('node -v');
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
140915 次 |
最近记录: |