Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

251
Visualizações
Run 3 bash scripts simultaneously then send their return values as JSON in REST API NodeJS

I'm trying to run 3 bash scripts depending on what the JSON list sent from the client specifies, then return their outputs to a JSON dict which will be sent to the client again.

This is the code of the three scripts:

marc@linux:~ $ cat 1.sh
sleep 1
echo -n "a"
marc@linux:~ $ cat 2.sh
sleep 1
echo -n "b"
marc@linux:~ $ cat 3.sh
sleep 1
echo -n "c"

If I executed them synchronously, they might stop the event loop for 3 seconds (undesirable):

const express = require("express");
const app = express();
app.use(express.json())

const cp = require("child_process");


app.get("/", (request, response) => {
    console.log(request.body);

    var response_data = {};
    if (request.body.includes("script1")) {
        response_data.value1 = cp.execFileSync("./1.sh").toString();
    }
    if (request.body.includes("script2")) {
        response_data.value2 = cp.execFileSync("./2.sh").toString();
    }
    if (request.body.includes("script3")) {
        response_data.value3 = cp.execFileSync("./3.sh").toString();
    }


    response.json(response_data);
    response.status(200)
    response.send()
})

app.listen(8080, () => {
    console.log("ready");
})

And if I executed them asynchronously, they would return after the response is sent, where the response would be just {}

My intended flow chart is that if I send ["script1", "script3"], it should return {"value1": "a", "value3": "c"} when the 1.sh and 3.sh are done executing, and without blocking the event loop. Example

How do I implement callbacks/promises in such scenario?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use Promise.all() for that:

const run = (script) => new Promise((resolve, reject) => {
    // spawns cp for script
    // without any args and options
    cp.execFile(`./${script.slice(-1)}.sh`, null, null, (res, err) => {
        if (err) reject(err);
        // returns res through callback
        resolve(res);
    });
});

const runScripts = (req) => new Promise((resolve, reject) => {
    const 
        result = {},
        promises = [];

    // creates promise for each script in request
    for (script of req) {
        // pushes it to the required array of promises for Promise.all
        promises.push(new Promise((resolve, reject) => {
            // runs script 
            run(script).then((res) => {
                // gets scripts result
                // and writes it
                result[`value${script.slice(-1)}`] = res;
                resolve();
            });
        }));
    };

    // uses Promise.all to wait until all scripts are done
    Promise.all(promises).then(() => {
        // when each script is done
        // finally returns final result
        resolve(result);
    });
});

// put req as your request.body
runScripts(req).then((res) => {
    // sends final result as response
    response.status(200).json(result);
}).catch((err) => {
    // if something goes wrong
    response.status(500).json('Something broke!');
});

I couldn't check this code because because I have Windows. If I try to execute scripts with child_process, it's tells me that I'm trying to execute UNKNOWN, even if I used sync function in test without anything, just console.log(cp.execFileSync("./1.sh").toString());. But, it worked for you.

So try it and tell me if it works or not.

P.S. Edited for error handling.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda