简体   繁体   English

一段时间后NodeJS exec停止运行而没有错误

[英]NodeJS exec stop after some time without error

I'm using exec from child_process . 我正在使用child_process exec

The function runs fine but after 4-5minutes, it just stops, without any errors reported, but the script should run for at least 24hours... 该函数运行正常,但是在4-5分钟后,它只是停止了,没有报告任何错误,但是脚本应该至少运行24小时...

Here is the code : 这是代码:

import { exec } from 'child_process';

function searchDirectory(dirPath) {
    let lineBuffer = '';

    const cmd = `find ${dirPath} -type f -name "*.txt" | pv -l -L 10 -q`;
    const findData = exec(cmd);

    findData.on('error', err => log.error(err));
    findData.stdout.on('data', data => {
        lineBuffer += data;
        let lines = lineBuffer.split('\n');
        for (var i = 0; i < lines.length - 1; i++) {
            let filepath = lines[i];

            processfile(filepath);
        }
        lineBuffer = lines[lines.length - 1];
    });
    findData.stdout.on('end', () => console.log('finished finding...'));
}

The pv command slows down the output, I need this since the path where I'm finding is over the network and pretty slow (60mb/s). pv命令会减慢输出速度,因为我要查找的路径是通过网络并且非常慢(60mb / s),所以我需要这样做。

When I run the command directly in the terminal it works fine (I didn't wait 24hours but I let it for half hour and it was still running). 当我直接在终端中运行命令时,它可以正常工作(我没有等待24小时,但我让它呆了半个小时,它仍然在运行)。

The processfile function actually makes an async call with axios to send some data to a server : 实际上,processfile函数使用axios进行异步调用以将一些数据发送到服务器:

    let data = readFileSync(file);
    ...
    axios.post(API_URL, { obj: data }, { proxy: false })
        .then(res => {
            log.info('Successfully saved object : ' + res.data._id);
        })
        .catch(err => {
            log.error(err.response ? err.response.data : err);
        });

What could cause the script to stop? 是什么导致脚本停止? Any ideas? 有任何想法吗?

Thanks 谢谢

I found the issue, using exec is not recommended for huge outputs since it's using a limited size buffer. 我发现了问题,不建议将exec用于大型输出,因为它使用的是有限大小的缓冲区。 Use spawn instead : 使用spawn代替:

The most significant difference between child_process.spawn and child_process.exec is in what they return - spawn returns a stream and exec returns a buffer. child_process.spawn和child_process.exec之间最大的区别在于它们返回的内容-spawn返回流,而exec返回缓冲区。

child_process.spawn returns an object with stdout and stderr streams. child_process.spawn返回带有stdout和stderr流的对象。 You can tap on the stdout stream to read data that the child process sends back to Node. 您可以点击stdout流以读取子进程发送回Node的数据。 stdout being a stream has the "data", "end", and other events that streams have. 作为流的stdout具有“数据”,“结束”和流具有的其他事件。 spawn is best used to when you want the child process to return a large amount of data to Node - image processing, reading binary data etc. 当您希望子进程将大量数据返回到Node时,最好使用spawn-图像处理,读取二进制数据等。

child_process.exec returns the whole buffer output from the child process. child_process.exec返回子进程的整个缓冲区输出。 By default the buffer size is set at 200k. 默认情况下,缓冲区大小设置为200k。 If the child process returns anything more than that, you program will crash with the error message "Error: maxBuffer exceeded". 如果子进程返回的值超过此值,则您的程序将崩溃,并显示错误消息“错误:超出了maxBuffer”。 You can fix that problem by setting a bigger buffer size in the exec options. 您可以通过在exec选项中设置更大的缓冲区大小来解决该问题。 But you should not do it because exec is not meant for processes that return HUGE buffers to Node. 但是您不应该这样做,因为exec不适用于将巨大缓冲区返回给Node的进程。 You should use spawn for that. 您应该为此使用spawn。 So what do you use exec for? 那么,您使用exec做什么呢? Use it to run programs that return result statuses, instead of data. 使用它来运行返回结果状态而不是数据的程序。

from : https://www.hacksparrow.com/difference-between-spawn-and-exec-of-node-js-child_process.html 来自: https : //www.hacksparrow.com/difference-between-spawn-and-exec-of-node-js-child_process.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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