Linux uses node.js to execute commands.

  

node.js can be used not only to write scripts for web pages, but also to run Linux commands on the server. The advantage of running the Linux node.js command line command is that it is fast and efficient. This article will introduce the method of executing linux commands using the node.js command line.

var sys = require (& lsquo; sys & rsquo;)

var exec = require (& lsquo; child_process & rsquo;) exec;.

//executes `pwd`

exec(“pwd”, function (error, stdout, stderr) {

sys.print(‘stdout: ’ + stdout);

sys. Print(‘stderr: ’ + stderr);

if (error !== null) {

console.log(‘exec error: ’ + error);

}

});

There is a need to execute shell commands from the front-end operations server

Creating a process.js file

Need to execute shell commands from the front-end operation server

Create a process.js file

var process = require(‘child_process’);

//Direct command

export.createDir = function (){process.exec(‘D: && cd testweb && md mydir’,

function Error, stdout, stderr) {

if (error !== null) {

console.log(‘exec error: ’ + error);

}

});

}

//Invoke Execution File

exports.openApp = function(){

process.execFile (‘D:/testweb/aaa.bat’,null,{cwd:‘D:/’},

function (error,stdout,stderr) {

if ( Error ! == null) {

console.log(‘exec error: ’ + error);

}

});

}< Br>

The command here is dead. If you need to call it dynamically, write the command as a batch file (linux write shell script)

You can also use process.exec(‘test.bat’, ...) and process.exec(‘sh test’,...) Execute file

The command here is write dead. If you need dynamic call, write the command as a batch file (linux write shell) Script)

You can also use process.exec(‘test.bat’,...) and process.exec(‘sh test’,...) to execute the file

Is to use the node.js command line to execute the linux command method, master this method, whether it is executed on the server or the host can be more comfortable.

Copyright © Windows knowledge All Rights Reserved