node.js - nodejs运行系统命令时遇到(Y or N)必须要输入Y或者N才能继续向下运行的解决办法是什么?
大家讲道理
大家讲道理 2017-04-17 14:43:56
0
2
805

假设当前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,然后回车运行。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
小葫芦

The

sudo command has a -S option, which is used to read the password when a password needs to be entered.

Assuming the password is 111111, then the complete command is as follows

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

Correspondingly, the node code can be like this

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);
    }
});
PHPzhong

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.

Because the output of sudo asking for a password does not flush the buffer and the node data is not triggered, the following code does not work

If the child process calls flush on the buffer (e.g. in python sys.stdin.flash), then theoretically code similar to this section can work

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}`);
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template