【发布时间】:2018-11-28 13:49:28
【问题描述】:
我正在使用来自child_process 的exec。
该函数运行良好,但在 4-5 分钟后,它就停止了,没有报告任何错误,但脚本应该运行至少 24 小时...
代码如下:
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...'));
}
pv 命令会减慢输出速度,我需要这个,因为我找到的路径是通过网络并且速度非常慢(60mb/s)。
当我直接在终端中运行命令时,它运行良好(我没有等待 24 小时,但我让它等待了半小时,它仍在运行)。
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);
});
什么可能导致脚本停止?有什么想法吗?
谢谢
【问题讨论】:
标签: javascript node.js bash file exec