Linux使用node.js执行命令的方法


node.js不仅可以用来编写网页的脚本,而且还能在服务器上运行Linux命令。Linux node.js命令行运行命令的优点是响应速度快又高效。本文就来介绍一下使用node.js命令行执行linux命令的方法。

var sys = require(‘sys’)

var exec = require(‘child_process’).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);

}

});

有需要从前端操作服务器执行shell命令的需求

建立一个process.js文件

有需要从前端操作服务器执行shell命令的需求

建立一个process.js文件

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’,...)执行文件

这里的命令是写死的,如果需要动态调用就把命令写成批处理文件(linux写shell脚本)

也可以使用process.exec(‘test.bat’,...) 和 process.exec(‘sh test’,...)执行文件


上一篇:RedHat 6使用xrdp登录Windows远程桌面的方法

下一篇:Linux利用script命令保存用户操作记录的方法


node.js执行linux命令 node.js命令行 node.js执行linux命令node.js命令行linuxnode.js安装node.js
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器