关于node使用child_process运行bat文件的问题
发布于 10 年前 作者 sulamiann 12016 次浏览 最后一次编辑是 8 年前 来自 问答

最近在使用childprocess模块 运行外部bat文件的时候遇到了问题。描述如下:

var child_process = require('child_process');
//调用执行文件
var openApp = function(){
    child_process.execFile('D:/testweb/1.bat',null,{cwd:'D:/'},function (error,stdout,stderr) {
        if (error !== null) {
          console.log('exec error: ' + error);
        }
        else console.log('成功执行指令!');
    });
}
openApp();
console.log('正在执行bat文件……');

bat文件很简单:

[@echo](/user/echo) off
echo 正在复制…… 
copy e:\c.txt d:\2.txt
exit

这个是可以运行成功的,复制了文件。 但是我注意到bat文件里面的“正在复制”没有在按照预料中的输出,请问一下,这是为什么? 另外,当我改变了bat文件里面的指令,改成杀进程的一段代码:

[@echo](/user/echo) off 
echo 正在清除您要kill的进程,请稍等...... 
taskkill /f /im sogoucloud.exe /t
taskkill /f /im AppleMobileDeviceService.exe /t
taskkill /f /im iPodService.exe /t
taskkill /f /im iTunesHelper.exe /t
taskkill /f /im QvodTerminal.exe /t
taskkill /f /im AlipaySecSvc.exe /t
taskkill /f /im D4Ser_ICBC.exe /t
taskkill /f /im vmware-usbarbitrator.exe /t
taskkill /f /im omniaddrservice.exe /t
taskkill /f /im armsvc.exe /t
taskkill /f /im wdkeymonitorccb.exe /t
taskkill /f /im vmnetdhcp.exe /t
taskkill /f /im dhmachinesvc.exe /t
taskkill /f /im vmware-tray.exe /t
taskkill /f /im vmware-authd.exe /t
taskkill /f /im google+ auto backup.exe /t
echo 清除系统进程完成! 
echo. & pause 

再次运行js文件,就死活运行不了这个bat了,很是不解,难道node只能运行简单的bat代码吗?还请高手告知原因,谢谢

6 回复

杀进程 有权限问题吗?

@leapon 应该不涉及吧,不清楚,要是验证是这个问题的话,应该怎么做呢?

@sulamiann 命令行下运行 bat 文件,不出错就不是权限问题。

@leapon 命令行下运行没有问题,可以正常运行,那么还想请问,权限问题怎么在node里面设置呢?

输出的问题在回调里加上:

console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);

然后看后面的问题具体是什么错误提示吧

@ravenwang 什么输出都没有,空字符串,这是什么原因? nodenode.jpg

回到顶部