Skip to content
Advertisement

Node.js child process exec returning : Error: kill EPERM STDERR STDOUT_SIZE

I am exposing and API dedicated to printing logs from a Linux System D service. Here is a sample of the code:

'use strict';
const exec = require('child_process').exec;
const express = require('express');

var router = express.Router();
router.get('/printlogs', function (req, res) {

//simplified for Stack Overflow
var completeCmd = "sudo journalctl -u node-myapp@serveruser --no-pager --since "2020-06-31 00:00:00"";

exec(completeCmd, (error, stdout, stderr) => {

    var status = 0;
    var response = "";

    if (error) {
        console.log("Error !");
        console.log("ERROR : " + error);
        console.log("STDERR : " + stderr);
        console.log("STDOUT_SIZE : " + stdout.length);
        status = 500;
        response = stderr;
    } else {
        console.log("Success !");
        status = 200;
        response = stdout;
    }

    res.contentType('text/plain').status(status);
    res.send(response);
    });
});
module.exports = router;

If the date in “completeCmd” is close enough (ex: last 10 days), everything works… if the date is too far back (ex: 3 months ago), the process fails in a weird way:

Error !
ERROR : Error: kill EPERM
STDERR : 
STDOUT_SIZE : 1046013

The response is returned in about 187 ms, so it is not a timeout… Theories:

  • Could it be a limitation in the possible size of stdout ?
  • Could it be linked to the fact that my command use sudo ?

Thanks !

Advertisement

Answer

Reading the error message, it looks like the error is because your standard output size is bigger than the default value in NodeJS.

Based on this documentation, you can increase the output buffer size as follows:

const bufferSize = 1024 * 2048; // (default 1024 * 1024, double up)
exec(command, {maxBuffer: bufferSize}, function(error, stdout, stderr){ 
  callback(error, stdout); 
});

Furthermore, you can always check the output byte size to confirm that bufferSize is the problem. This helps to determine if the maxBuffer needs to be incremented.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement