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

282
Visualizações
Nodejs: Exec from child_process returns bin/sh: 1: command not found but works when writing manually in terminal?

Using Node.js to automatically execute command in the terminal on raspberry pi, tho it wont work with exec(command). It outputs bin/sh: 1: command not found when trying to catch the output. But the command works when writing the command manually in the terminal?

Why is that?


async function run_command_fuel() {
    const command = "weconnect-cli PASSWORDINFORMATION get /vehicles/SECRETNUM/domains/fuelStatus/rangeStatus/primaryEngine/remainingRange_km";
   
    let returnval = 0;

    let child = exec(command);


    await new Promise((resolve, reject) => {
        child.stdout.on('data', function(data) {
            console.log('stdout: ' + data);
            returnval = data;
            console.log(returnval);
            resolve();
        });
        child.stderr.on('data', function(data) {
            console.log('stderr: ' + data);
            reject();
        });
        child.on('close', function(code) {
            console.log('closing code: ' + code);
        });
    })

    return returnval;

}

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

0

Running the exec command creates a new shell, which wont have the same environment as your node process. You will need to specify the PATH environment variable in order to use that command.

Node gives you a way to do this through the object you pass in the exec command, as the second parameter, which defaults to process.env.

To pass in the object:

const command = "weconnect-cli PASSWORDINFORMATION get /vehicles/SECRETNUM/domains/fuelStatus/rangeStatus/primaryEngine/remainingRange_km";

let child = exec(command, {env: {'PATH': 'path/to/command'}});

Another way you can do this is through standard shell env setting before a command. You can modify your command to this:

const env_vars = 'PATH=' + 'path/to/command' + ' ';
const command = "weconnect-cli PASSWORDINFORMATION get /vehicles/SECRETNUM/domains/fuelStatus/rangeStatus/primaryEngine/remainingRange_km";

let child = exec(env_vars + command);
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