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();
});
Related
I'm trying to restart a node.js server without any thrid party library. The bat file is executed from the node server itself.
Here is the restart.bat file:
set processId=%1
taskkill /F /PID %processId%
node app.js
Here is the code which is supposed to run the bat file:
var exec = require('child_process').exec;
exec('restart.bat ' + process.pid,
function (error, stdout, stderr) {
console.log('error: ' + error);
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
}
);
With the code above, the process is killed but the server never restart.
Can someone help please ?
PS: my node version is v8.11.4
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).
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);
I want to execute multiple Shell commands from a node.js application consecutively. In my case, exec(command1 & command2) does not work as it should. It works for simple commands like ls and pwd, but I need to start a server and then execute special commands on it and there, it only executes the very first command and nothing more. When I work on my command-line interface, I just type in one command after the other and I need a facility to execute them in the same manner, but automatically starting from a node.js application - and all of them in the same process!
You could use child_process module to achieve that.
Here's an example code to demonstrate the usage
var child_process = require('child_process');
var task1 = child_process.exec('node --version', function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
var task2 = child_process.exec('npm --version', function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
I am trying to create an webscket control wrapper for mplayer on beaglebone with node.js.
A glimpse of my websocket server looks lke this:
io.sockets.on('connection', function (socket)
{
SaveToLog('Client ' + clientAddress + ' connected.');
/* Function used to control the LEDs according to HTML file */
allClients.push(socket);
socket.on('miro_server', function (device, command)
{
SaveToLog(device + ' ' + command);
switch (device)
{
case 'mplayerStatus':
if(command == 'PLAYSELECTED')
{
ls = childProcess.exec('mplayer -slave -quiet -input file=/home/root/.mplayer/mplayer_fifo /home/root/agalloch.mp3',
function (error, stdout, stderr)
{
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null)
{
console.log('exec error: ' + error);
}
});
ls.on('exit', function (code)
{
console.log('Child process exited with exit code '+code);
});
}
else /* PAUSE */
{
childProcess.exec('echo "pause" > /home/root/.mplayer/mplayer_fifo',
function (error, stdout, stderr)
{
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null)
{
console.log('exec error: ' + error);
}
});
}
break;
}
});
});
The mplayer process starts correctly and I can hear music from the speakers, but the problem is that I cannot send any commands to the mplayer process via the selected fifo.
When I try the same commands in the linux terminal, it works just fine, so I presume it has something to do with the node.js process exec part.
I do not have much experience with linux or node.js, and most of the code I've written is based on code examples and tutorials.
Can anyone explain what is going on and why I cannot send playback command via fifo to the mplayer process using node.js?
Thank you.
you need to add new line to the echo command
'echo "pause" > /home/root/.mplayer/mplayer_fifo\n'
I tried this on rpi and bbb and it works info here http://sonnycruz.blogspot.com