Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!