Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
How to run child_process.exec correctly on an ajax request?

There is a server, that I have an access to, but do not have ownership on. It serves a node js / express application on a default port 3000. There are several scripts, that are usually run either manually from the terminal or by cron job. What I want to do is to have a button on the client-side and make an ajax request to a certain route and execute a node js command with inline arguments. For example:

node script.js 123

All routes are set and working. I have a CliController file that handles requests and has to run the command above. Currently I am using the following code:

cp.exec(`node script.js ${ip}`, function (err, stdout, stderr) {
      if (err) {
         console.log(err);
      }
      console.log(stdout);
      console.log(stderr);
 });

The script.js file is in root folder of the project, but the project itself was built by using express-generator and is being served using node bin/www command. There is a service/process on the server that runs nodemon to restart this project if it fails as well. Therefore I do not have access to output of that particular process.

If I run the command above in the terminal (from the root folder of the project, to be precise), it works fine and I see the output of the script. But if I press the button on the webpage to make a request, I am pretty sure that the script does not execute, because it has to make an update to database and I do not see any changes. I also tried to use child_process.spawn and child_process.fork and failed to get it working.

I also tried to kill nodemon and quickly start the project again to the see console output. If I do this, everything works.

What am I doing wrong ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The process invoked may be in a blocking state, hence the parent script is simply waiting for the children process to terminate, or return something.

We can avoid this behaviour right into the shell command, by adding & (ampersand control operator) at the end.

This makes a command running in the background. (Notice, you can still control the children(s) process using the PID's and POSIX signals, this is another subject, but very related and you might find it very handy pretty soon).

Also notice that killing/stopping the parent script will also kill the children(s). This can be avoided using nohup.

This is not linked to JavaScript or node.js, but to bash, and can be used with anything in the shell.

cp.exec(`node script.js ${ip} &`, function (err, stdout, stderr) {
      if (err) {
         console.log(err);
      }
      console.log(stdout);
      console.log(stderr);
 });

Bash reference manual

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda