suchen

Heim  >  Fragen und Antworten  >  Hauptteil

node.js - nodejs运行系统命令时遇到(Y or N)必须要输入Y或者N才能继续向下运行的解决办法是什么?

假设当前nodejs要运行命令rm -rf 123.txt,那么代码就是

process.exec('rm -rf 123.txx',function (error, stdout, stderr) {
    if (error !== null) {
        console.log('exec error: ' + error);
    }else {
        console.log(stdout)
    }
});

OK,这里确实删除了。但是如果加上sudo呢?比如sudo rm -rf 123.txt,需要输入密码才能删除。下面是我尝试的办法,但是无法删除:

process.exec('sudo rm -rf 123',function (error, stdout, stderr) {
    if (error !== null) {
        console.log('exec error: ' + error);
    }else {
        process.exec('密码',function (error, stdout, stderr) {
            if (error !== null) {
                console.log('exec error: ' + error);
            }else {
                console.log(stdout)
            }
        });
        console.log(stdout)
    }
});

这是我尝试嵌套process.exec来解决,但是发现无法解决。

上面是个例子,实际项目中我需要使用nodejs来调用python或者其他脚本。
比如xxx.py,在实际终端下他是这样的:

test@test:~/$ python xxx.py
info:xxxxxxx
请问是否继续么?(Y or N):Y
sys_info:xxxxx
请问是否退出?(Y or N):N
log_info:xxxxx
test@test:~/$ 

如果nodejs调用的话,该怎么办,怎么让提示信息为请问是否继续么?(Y or N):时自动输入Y,并回车继续向下执行。当提示信息为请问是否退出?(Y or N):时,自动输入N,然后回车运行。

大家讲道理大家讲道理2782 Tage vor740

Antworte allen(2)Ich werde antworten

  • 高洛峰

    高洛峰2017-04-17 14:45:56

    sudo命令有个-S选项,用于在需要输入密码的时候,读取密码。

    假设密码为111111,那么,完整命令如下

    echo "111111" | sudo -S rm -rf ./123.txt

    相应的,node代码可以这样

    var child_process = require('child_process');
    child_process.exec('echo "111111" | sudo -S rm -rf ./123.txt', function(error, stdout, stderr){
        if(error){
            throw error;
        }else{
            console.log(stdout);
        }
    });

    Antwort
    0
  • PHPz

    PHPz2017-04-17 14:45:56

    stackoverflow

    I have been working on this all day.

    The problem is that the STDOUT of your spawned process needs to flush it's output buffer, otherwise it will sit there until it fills up, in which case your code won't execute again.

    p.stdin.end() only serves to end the process, which by its very nature, allows the OS to clear up all buffers.

    You can't do this from the node as this is not the owner of the output buffer.

    It is annoying, but as long as you have control of the script, you can modify it there, perhaps allow it to take a command line option to set an autoflush?

    Hope this is of some help.

    由于sudo索要密码的输出没有冲刷缓冲区,node的data没有被触发,所以下面的代码不能工作

    假如子进程调用了对缓冲区的flush(e.g. in python sys.stdin.flash),那么理论上类似这一段的代码可以工作

    const childProcess = require("child_process");
    
    const exec = childProcess.spawn("sudo", ["rm", "123.tx"], {
      stdio: "pipe"
    });
    
    exec.stdout.on("data", (data) => {
      console.log("stdout", data.toString());
      exec.stdin.write("123456\n");
    });
    
    exec.stderr.on("data", (data) => {
      console.log("stderr", data.toString());
      exec.stdin.write("123456\n");
    });
    
    exec.on("close", (code) => {
      console.log(`exit ${code}`);
    });

    Antwort
    0
  • StornierenAntwort