简体   繁体   中英

Nodejs: child_process.exec get error when command return nothing

When using child_process.exec in nodejs, it hit an error when the command return nothing. I do following:

const {exec} = require('child_process');

const cmd = `ps -ef | grep -v grep | grep abc123.py`;
exec(cmd, (err, stdout, stderr) => {
    if(err) {
        console.error(`__get error: ${stderr}`);
        return;
    }
    console.log(stdout);
    return;
})

Since 'abc123.py' is not running, it return nothing if run this command directly. But this code get this:

__get error:

I meet this error with Node 8.10.0 and 10.16.0 . Is there anything I ignored?

You try to exec nonexistent script, so your ps -ef | grep -v grep | grep abc123.py ps -ef | grep -v grep | grep abc123.py ps -ef | grep -v grep | grep abc123.py return 1 as a exit code and write nothing to stderr. From Nodejs.org we know that the

Any exit code other than 0 is considered to be an error.

So, your code works right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM