Skip to content

Instantly share code, notes, and snippets.

@pghalliday
Created November 20, 2012 10:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pghalliday/4117163 to your computer and use it in GitHub Desktop.
Save pghalliday/4117163 to your computer and use it in GitHub Desktop.
node.js windows spawn with cmd /s /c
var http = require('http');
var spawn = require('child_process').spawn;
var child = spawn(
'CMD', [
'/S',
'/C',
'node',
'./child.js'
]
);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.on('exit', function() {
http.get('http://localhost:8080', function(response) {
console.log('child did NOT die');
}).on('error', function(error) {
console.log('child did die');
});
});
setTimeout(function() {
http.get('http://localhost:8080', function(response) {
response.on('end', function() {
console.log('kill the child');
child.kill();
});
});
}, 2000);
var http = require('http');
var PORT = 8080;
var server = http.createServer(function(request, response) {
response.end();
});
server.listen(PORT, function() {
console.log('Listening on port ' + PORT);
});
V:\GitHub\node-ChildDaemon\test\Spikes\windows>node test.js
Listening on port 8080
kill the child
child did NOT die
*********
I can kill the process and children with ctrl-c at this point (although i expected it to kill the child and exit on it's own)
@Breme
Copy link

Breme commented Feb 7, 2017

Can you Help me on this ?

session.on('exec', function (accept, reject, info) {
console.log('Client wants to execute: ' + inspect(info.command));
var stream = accept();

                 var cp = spawn('XXXCLI 10.21.254.12', {shell: true});

                 stream.stdin.pipe(cp.stdin);

                 cp.stdout.pipe(stream.stdout);

            sleep(6000);
                 cp.stderr.pipe(stream.stderr);

                 cp.on('exit', function (code, signal) {
                 stream.exit(signal || code);
                 }).on('end', function (code, signal) {
                 stream.close();

                 });


        });

When I manually type the first command 'XXXCLI ip_address' in my command prompt and press enter,I will get a output "Connected to CLI...." .Once I get this connection successful, I need to execute my second command i.e "Lmc sample" which will load the master config and I will get the output as "Message sent..", third command will execute a script,will get output as "Message sent.." .This is what happens when I enter these commands manually in cmd prompt and execute.

What is happening is once I execute my first command i.e "XXXCLI 10.21.254.12" manually in cmd, The path where we actually execute the commands i.e( C:\users\CLI>) will not be visible. This happens because now it got connected with the above mentioned ip (10.21.254.12) .And Only after connecting to this ip ,I can able to execute my other commands.i.e command to load master config ,cmd to execute script etc.

So I want to execute my first command and get its stream in a variable and execute rest of the commands inside the stream created by first command Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment