NodeJs exec with ssh not working with forever - node.js

below is the code with is working fine with node test.js command. but whenever i am trying to run test.js with forever utility it wont work.
var terminal = require('child_process').spawn('bash');
terminal.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
terminal.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
setTimeout(function() {
console.log('Sending stdin to terminal');
terminal.stdin.write("whomi\n");
terminal.stdin.write("sudo ssh instance-solr-1.us-central1-b.versatile-nomad-126504 \"sudo su | . /root/file
.sh >>/dev/null 2>/dev/null\" &\n");
console.log('Ending terminal session');
terminal.stdin.end();
}, 1000);

Related

SIGTERM in node.js on Windows

I am new to node.js and following some tutorials. In one of them, I execute the following code and expect to shutdown the terminal gracefully which is not the case
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
process.stdout.write('Data! -> ' + chunk);
});
process.stdin.on('end', function() {
process.stderr.write('End!\n');
});
process.on('SIGTERM', function() {
process.stderr.write("Why are you trying to terminate me?!? :-)");
});
console.log("Node is running as process #" + process.pid);
It works fine, but when I issue, from another terminal the following
taskkill /PID 29884 /F
I don't get the function
process.on('SIGTERM', function()....
get executed.
I came across a thread here at
What is the Windows equivalent of process.on('SIGINT') in node.js?
I get the same behavior, killing the process just goes back to the command line
Then I tried to update the code I got from the thread with some code in order to reprint the Data I enter on the console, it is not getting there (I added rl.on("data", function(chunk))
(process.platform === "win32") {
var rl = require("readline").createInterface({
input: process.stdin,
output: process.stdout
});
rl.output.setEncoding('utf8');
console.log("Node is running as process #" + process.pid);
rl.on("data", function(chunk) {
this.output.write('Data! -> ' + chunk);
});
rl.on("SIGINT", function() {
process.emit("SIGINT");
});
};
process.on("SIGINT", function() {
//graceful shutdown
process.exit();
});
The
rl.on("data", function(chunk) {
this.output.write('Data! -> ' + chunk);
});
just sends back 'Data! -> ' string without the text I enter in the console as the case with the 1st code from the tutorial. What is missing?
Of course, In both cases
process.stderr.write("Why are you trying to terminate me?!? :-)");
is not getting executed
Thanks

node child_process gives valid output in stderr for mongodump

I'm executing mongodump command from nodejs child_process.
I have tried both exec and spawn, but the progress printed by mongodump is hitting the data event of stderr instead of stdout
var exec = require('child_process').exec,
ls = exec('mongodump --gzip --archive="/home/test-machine/test.archive" --db myDB');
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code.toString());
});
The mongodump is not crashing, or throwing any error, its executing as it should. But somehow nodejs is treating the output as stderr
mongodump writes log messages to stderr, which isn't very uncommon, although it makes parsing the output for errors a bit harder because you need to manually filter out the error messages.

webpack child_process.exec - TypeError: error is not a function

Trying to execute child_process.exe using webpack but getting error that exec is not a function. The script is working in node prompt.
test.js
const chai = require('chai');
const exec = require('child_process').exec;
describe('node version', function nodeVersion() {
it('Should display Node Version', (done) => {
exec('node -v', function(error, stdout, stderr) {
console.log('stdout: ', stdout);
console.log('stderr: ', stderr);
if (error !== null) {
console.log('exec error: ', error);
}
done();
});
});
});
Is there anyway we can execute shell commands like child_process.exec on browser?
No, there is no way to run child_process in the browser because JavaScript in the browser is not allowed to create processes. Thankfully there is no way to run a shell script in the browser, otherwise any website would have access to your machine and the bad things you could do with it are endless (e.g. delete everything with rm -rf / or at least what you've permission for).

Restart current instance using NodeJS

I'd like to restart my application inside this application using NodeJS and NPM.
It doesn't work with child_process :
var exec = require('child_process').exec;
exec('npm restart', function(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
If your Master process died, there is no way to reanimate him by himself.
Have a look at Nodemon to restart your script.
Another option: you can use forever npm module in order to start and monitor app.
So the restart function like this if from api:
app.get('/restart', function (req, res, next) {
process.exit(1);
});
Or if you are using a cluster approach you can kill the child and fork new one as shown below [checkout cluster documentation for node,
cluster.on('exit', function (worker) {
logger.info('Worker ' + worker.id + ' died :(, starting another one.');
cluster.fork();
});

Node.js detect a child process exit

I am working in node, as it happens via a visual studio code extension. I successfully create child processes and can terminate them on command. I am looking to run code when the process unexpectedly exits, this appears to be what the "exit" event is intended for, but I'm unclear on how to call it, this is the code I am working with, the process runs, but does not detect/log on exit, note that output.append is visual studio code specific version of console.log():
child = exec('mycommand', {cwd: path},
function (error, stdout, stderr) {
output.append('stdout: ' + stdout);
output.append('stderr: ' + stderr);
if (error !== null) {
output.append('exec error: ' + error);
}
});
child.stdout.on('data', function(data) {
output.append(data.toString());
});
Here's four things I have tried that do not work in logging on exit:
child.process.on('exit', function(code) {
output.append("Detected Crash");
});
child.on('exit', function(code) {
output.append("Detected Crash");
});
child.stdout.on('exit', function () {
output.append("Detected Crash");
});
child.stderr.on('exit', function () {
output.append("Detected Crash");
});
Looking at the node.js source code for the child process module, the .exec() method does this itself:
child.addListener('close', exithandler);
child.addListener('error', errorhandler);
And, I think .on() is a shortcut for .addListener(), so you could also do:
child.on('close', exithandler);
child.on('error', errorhandler);

Resources