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

108
Vistas
NodeJS Fork can't get childprocess to kill

I'm running against a wall here, maybe it's just a small problem where I can't see the solution due to my inexperience with NodeJS.

Right now I'm constructing a BT device which will be controlled by a master application and I have settled for the prototyping on a Raspberry PI 3 with NodeJS using the Bleno module.

So far everything worked fine, the device gets found and I can set and get values over Bluetooth. But to separate the different "programs" which the device could execute from the Bluetooth logic (because of loops etc.) I have opted to extract these into external NodeJS files.

The idea here was to use the NodeJS fork module and control the starting and stoppping of those processes through the main process.

But herein my problems start. I can fork the different JavaScript files without problem and these get executed, but I can't get them to stop and I don't know how to fix it.

Here's the code (simplified):

var util = require('util');
var events = require('events');
var cp = require('child_process');
...
var ProgramTypeOne = {
NONE:    0,
ProgramOne: 1,
...
};
...
var currentProgram=null;
...

function BLEDevice() {
  events.EventEmitter.call(this);
  ...
  this.currentProgram=null;
  ...
  }

util.inherits(BLELamp, events.EventEmitter);

BLELamp.prototype.setProgram = function(programType, programNumber) {
  var self = this;
  var result=0;

  if(programType=="ProgramTypeOne "){
    if(programNumber==1){
      killProgram();
      this.currentProgram=cp.fork('./programs/programOne');
      result=1;
    }else if(programNumber==2){
...
  }

  self.emit('ready', result);
};

...
module.exports.currentProgram = currentProgram;
...
function killProgram(){
  if(this.currentProgram!=null){
          this.currentProgram.kill('SIGTERM');
      }
}

There seems to be a problem with the variable currentProgram which, seemingly, never gets the childprocess from the fork call. As I have never worked extensivley with JavaScript, except some small scripts on websites, I have no idea where exactly my error lies. I think it has something to do with the handling of class variables.

The starting point for me was the Pizza example of Bleno.

Hope the information is enough and that someone can help me out.
Thanks in advance!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Since killProgram() is a standalone function outside of the scope of BLELamp, you need to call killProgram with the correct scope by binding BLELamp as this. Using apply (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) should resolve it. The following I would expect would fix it (the only line change is the one invoking killProgram):

BLELamp.prototype.setProgram = function(programType, programNumber) {
  var self = this;
  var result=0;

  if(programType=="ProgramTypeOne "){
    if(programNumber==1){
      killProgram.apply(this);
      this.currentProgram=cp.fork('./programs/programOne');
      result=1;
    }else if(programNumber==2){
...
  }

  self.emit('ready', result);
};

As a side note, your code is kind of confusing because you have a standalone var currentProgram then a couple prototypes with their own bound this.currentPrograms. I would change the names to prevent confusion.

over 4 years ago · Santiago Trujillo 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