nodejs 執行shell 命令


有需要從前端操作服務器執行shell命令的需求

建立一個process.js文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var  process = require( 'child_process' );
//直接調用命令
exports.createDir =  function  (){process.exec( 'D: && cd testweb && md mydir' ,
       function  (error, stdout, stderr) {
         if  (error !==  null ) {
           console.log( 'exec error: '  + error);
         }
     });
}
//調用執行文件
exports.openApp =  function (){
     process.execFile( 'D:/testweb/aaa.bat' , null ,{cwd: 'D:/' },
       function  (error,stdout,stderr) {
         if  (error !==  null ) {
           console.log( 'exec error: '  + error);
         }
     });
}

這里的命令是寫死的,如果需要動態調用就把命令寫成批處理文件(linux寫shell腳本)

也可以使用process.exec('test.bat',...) 和 process.exec('sh test',...)執行文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM