nodejs child_process child process run twice
Asked Answered
P

1

7

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!

Philippi answered 8/6, 2015 at 8:59 Comment(4)
I don't understand the comment about "after two minutes, it execute again". What does that mean? Also, you are calling .kill() too soon before your child_process has finished .exec() so you aren't giving it a chance to run.Weiweibel
@Weiweibel but it actually run twice, if child_process create a process , it run successfully. Does it terminate automatically ?Philippi
Whether it terminates automatically depends upon whether the command you're running terminates by itself or not. If mstsc normally runs and then terminates, then you should just let it do so without killing the process. The code you show here wouldn't run anything twice so if it is running twice, then it's because of some other code besides what you show here.Weiweibel
@Weiweibel You are right. I log my code execution,it actually execute twice, as you think,it is some other code that cause the problem. You save my life.Thank you very much!Philippi
S
2

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 ****');
});
Shakespeare answered 11/1, 2017 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.