在Node.js中执行多个shell命令

我想连续执行一个node.js应用程序的多个Shell命令。 在我的情况下, exec(command1 & command2)不工作,因为它应该。 它适用于像lspwd这样的简单命令,但是我需要启动一个服务器,然后在其上执行特殊命令,它只执行第一个命令,而不再执行其他命令。 当我在我的命令行界面上工作时,我只需键入一个命令,然后我需要一个工具以相同的方式执行它们,但是自动从一个node.js应用程序开始 – 所有这些都在同一个进程中!

你可以使用child_process模块来实现。

这是一个示例代码来演示使用情况

 var child_process = require('child_process'); var task1 = child_process.exec('node --version', function (error, stdout, stderr) { console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); if (error !== null) { console.log('exec error: ' + error); } }); var task2 = child_process.exec('npm --version', function (error, stdout, stderr) { console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); if (error !== null) { console.log('exec error: ' + error); } });