node.js执行shell命令

nodejs功能强大且多样,不只是可以实现 服务器端 与 客户端 的实时通讯,另一个功能是用来执行shell命令

首先,引入子进程模块

var process = require('child_process');

然后,调用该模块暴露出来的方法exec

process.exec('shutdown -h now',function (error, stdout, stderr) {
        if (error !== null) {
          console.log('exec error: ' + error);
        }
});

回调函数非必须!

原文地址:https://www.cnblogs.com/hf8051/p/4794627.html