Tuesday 16 May 2017

kill() function is not recognized for child_process

Using node js I'm calling external script in MATLAB and Python That is working well using terminal commands to run the scripts using those application ('start')

But when I'm trying to close them using kill() ('stop') I get an error:

TypeError: exec.kill is not a function

I'm using MAC OS and this is my code:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var path = require('path');

const exec = require('child_process').exec;
var cmd1 =
  '/Applications/MATLAB_R2016b.app/bin/matlab -nojvm  < /Users/dorsimon/Desktop/liftrack/Testing_Classifier.m';
var cmd2 = 'python /Users/dorsimon/Desktop/liftrack/arduino_sampling_for_workout.py';

app.get('/', function(req, res) {
  res.sendfile('index.html');
});

//Whenever someone connects this gets executed
io.on('connection', function(socket) {
  console.log('A user connected');

  fs.watch('/Users/dorsimon/Desktop/liftrack/result/', function(event, test) {
    console.log('event is: ' + event);
    fs.readFile('/Users/dorsimon/Desktop/liftrack/result/results.csv', 'utf-8', function read(
      err,
      data
    ) {
      if (err) {
        console.log('err');
        throw err;
      }
      console.log(data);
      socket.send(data);
    });
  });

  fs.watch('/Users/dorsimon/Desktop/liftrack/go', function(event, test) {
    console.log('event is: ' + event);
    fs.readFile('/Users/dorsimon/Desktop/liftrack/go/go.csv', 'utf-8', function read(err, data) {
      if (err) {
        console.log('err');
        throw err;
      }
      console.log(data);
      socket.send(data);
    });
  });

  //Whenever someone disconnects this piece of code executed
  socket.on('disconnect', function() {
    console.log('A user disconnected');
  });

  socket.on('start', function() {
    exec(cmd1, function(error, stdout, stderr) {
      // command output is in stdout
    });
    exec(cmd2, function(error, stdout, stderr) {
      // command output is in stdout
    });
  });

  socket.on('stop', function() {
    exec.kill();
  });
});

http.listen(3000, function() {
  console.log('listening on *:3000');
});

How can I kill those child_process that I started?

Thanks Dor



via user3523900

No comments:

Post a Comment