Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs child_process child process run twice

I want to connect a computer by the full computer name on Remote Desktop Connection. In nodejs, i create a child process to execute a cmd command.It executed successfully,but after two minutes, it execute again. I use the kill method of child_process module,it doesn't work.

    var child_process = require('child_process');
    child_process.exec('mstsc /v ' + fullName, function(err, stdout, stderr) {
        if(err){
            console.log(err);
        }           
    });
    child_process.kill();

Can you help me? Thank you very much!

like image 411
Edward Avatar asked Jun 08 '15 08:06

Edward


People also ask

How does Nodejs handle child process?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.

What is child_process spawn?

spawn returns an object with stdout and stderr streams. You can tap on the stdout stream to read data that the child process sends back to Node. stdout being a stream has the "data", "end", and other events that streams have.

Can we create child processes in node applications in node JS?

Node. js provides the fork() function, a variation of spawn() , to create a child process that's also a Node. js process. The main benefit of using fork() to create a Node.

How do I close a node js child process?

You can drop privileges after executing mongod (with process. setuid() and process. setgid() ) if you want.


1 Answers

Iv'e experienced the same issue, It took me a while to understand, the problem was the HTTP server and not the 'chileProccess'. The missing link in your question is the fact you run the the executeScript through an HTTP request (probably expressJs since you get the timeout after 2 min).

The problem: since it was not clear.

What was actually happening is that the HTTP request was reached the timeout boundary set by the HTTP server, which in expressJS is 2 min.

After the timeout, since there was no handling and the request did not closed, it was invoked once more, and so on each 2 min.

The solution:

server.setTimeout() is the method that sets the HTTP connection timeout for all connections.

The 2 minutes are default.

Example:

var express = require('express');
var http = require('http');

var app = module.exports.app = express();
var server = http.createServer(app);
server.setTimeout(10*60*1000); // 10 * 60 seconds * 1000 msecs
server.listen(appConfig.port, function () {
    var logger = app.get('logger');
    logger.info('**** STARTING SERVER ****');
});
like image 145
Adi Darachi Avatar answered Oct 20 '22 20:10

Adi Darachi