繁体   English   中英

在 node.js 中运行命令

[英]Run command in node.js

我正在像这样从 node.js 调用 php:

var exec = require('child_process').exec;

var result = exec('php index.php decrypt decrypt1 ' + s1, function(error, stdout, stderr) {

 });

您可以看到这是使用子进程。 但我不想使用子进程。 因为它是异步工​​作的。 如何在不使用子进程的情况下在 node.js 中运行 linux 命令?

您可以尝试ShellJS - Node.js 的 Unix shell 命令 它有同步和异步调用。 下面是一个例子:

require('shelljs/global');
exec('php -v').output;

当你运行这个文件时,你会得到:

PHP 5.5.24 (cli) (built: May 19 2015 10:10:05) 
Copyright (c) 1997-2015 The PHP Group 
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies 

如果要同步运行脚本,可以使用 child_process.execSync。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM