MongoDB database backup from docker container in node.js - node.js

My docker container is running on port 27018. When I run following command from terminal, it's working correctly -
docker exec mongodb mongodump -u=Abhishek -p=abhishek --authenticationDatabase=testdb
But, when I run the same command from node.js, it's not working. Please have a look into my below code and help me debugging 🙏🙏
import { spawn } from 'child_process';
const backupProcess = spawn('sudo docker exec mongodb mongodump', [
`-u=Abhishek`,
`-p=abhishek`,
`--authenticationDatabase=testdb`
]);
backupProcess.on('error', (err) => {
console.log('Error :: ', err);
});
backupProcess.on('exit', (code, signal) => {
if (code) {
console.log(MessageType.warn, '\n Backup process exited with code ', code);
} else if (signal) {
console.log(MessageType.error, '\n Backup process was killed with signal ', signal);
} else {
console.log(MessageType.success, '\n Successfully backed-up the database ✅ ');
}
});
It's giving me following error -
Error :: Error: spawn sudo docker exec mongodb mongodump ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:470:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn sudo docker exec mongodb mongodump',
path: 'sudo docker exec mongodb mongodump',
spawnargs: [
'-u=Abhishek',
'-p=abhishek',
'--authenticationDatabase=testdb'
]
}

Related

How to run exe file from node.js script?

I have an exe and I want to run it from node.js, and pass an argument, except that it doesn't work..
var exec = require('child_process').execFile;
const fs = require('fs');
try {
if (fs.existsSync("./program/bin/Release/program.exe")) {
console.log("Exists")
} else {
console.log("Not Exists")
}
} catch(err) {
console.log(err)
}
setTimeout(function() {
exec('./program/bin/Release/program.exe manual', function(err, data) {
console.log(err)
console.log(data.toString());
});
}, 0);
It definitely exists as it prints exists, and I can run it from a cmd prompt giving manual as an argument. But through node.js it is not working. It comes back with
Error: spawn ./program/bin/Release/program.exe manual ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn ./program/bin/Release/program.exe manual',
path: './program/bin/Release/program.exe manual',
spawnargs: [],
cmd: './program/bin/Release/program.exe manual'
}
Does anyone know?
Thanks
Arguments should be passed as an array of strings as the second argument, like so:
exec('./program/bin/Release/program.exe', ['manual'], function(err, data) {
console.log(err)
console.log(data.toString());
});
https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

Windows Fails to recognize installed commands while using child_process.exec() in nodejs

I am able to run 'ipconfig.exe' or 'netstat.exe' using exec but I am unable to run .bat command "chef" or "chef-apply".
OS: Windows 7
exports.testscript=function(req,res){
var exec = require('child_process').exec;
console.log("inside function");
var child = exec('chef-apply azurepro.rb' , {cwd :'C:\Users\xyz\chef-repo'},
function(error, stdout, stderr){
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(error);
return;
}
});
//child.stdin.end();
};
I am getting this error,
{ [Error: spawn cmd.exe ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn cmd.exe',
path: 'cmd.exe',
cmd: 'cmd.exe /s /c "chef-apply azurepro.rb"' }
From my understanding the bin path for command is not resolved. How can I fix this?

FFMpeg incorrect execution calling two different paths in NodeJS

I'm trying to take screenshots from a movie file and my app crashes with the following error:
$ FFMPEG_PATH=C:\\Apps\\ffmpeg\\bin\\ node .
=====Convert Video Failed======
{ [Error: spawn c:\Apps\ffmpeg\bin\ffprobe.exe
c:\Program Files (x86)\ImageMagick-6.8.3-Q16\ffprobe.exe ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe',
path: 'c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe',
spawnargs:
[ '-show_streams',
'-show_format',
'j:\\some.avi' ] }
stdout: undefined
stderr: undefined
As you can see I'm passing a the FFMPEG_PATH env variable because otherwise I'm getting a similar error:
$ node .
=====Convert Video Failed======
{ [Error: spawn c:\Apps\ffmpeg\bin\ffprobe.exe
c:\Program Files (x86)\ImageMagick-6.8.3-Q16\ffprobe.exe ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe',
path: 'c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe',
spawnargs:
[ '-show_streams',
'-show_format',
'j:\\some.avi' ] }
stdout: undefined
stderr: undefined
In both cases you can see that the command that node/fluent-ffmpeg is using results in a double path like this: c:\\Apps\\ffmpeg\\bin\\ffprobe.exe\r\nc:\\Program Files (x86)\\ImageMagick-6.8.3-Q16\\ffprobe.exe which obviously fails.
What causes this and how do I fix it?
Win 7, Node v4.1.2, ffmpeg version N-76041-g0418541
The code I'm using:
var ffmpeg = require('fluent-ffmpeg');
// ffmpeg.setFfprobePath("c:\\Apps\\ffmpeg\\bin\\ffprobe.exe");
var filename = 'j:\\some.avi';
var command = ffmpeg(filename);
// Code from an example
command
.on('filenames', function(filenames) {
console.log('Will generate ' + filenames.join(', '))
})
.on('end', function() {
console.log('Screenshots taken');
})
.on('error', function(err, stdout, stderr) {
console.log(" =====Convert Video Failed======");
console.log(err);
console.log("stdout: " + stdout);
console.log("stderr: " + stderr);
})
.screenshots({
// Will take screens at 20%, 40%, 60% and 80% of the video
count: 4,
folder: 'd:\\projects\\pics'
})
It only happens in Git Bash - which I got used to working in window with. Running the script in normal cmd works properly.

executing the bash on the amazon server with a error [Error: spawn EACCES], the bash is in right path

I am using the gith to build a webhook server on the amazon server to automate the deployment. When I updated my repository, the gith server can receive the update, then I want to execute the bash.
The bash file is on the path of /home/ubuntu/node/githook/hook.sh, console.log(__dirname+'/hook.sh'); output the right path, but when using the execFile to execute the path, it gave a error
error { [Error: spawn EACCES] code: 'EACCES', errno: 'EACCES', syscall: 'spawn' }
The code is above, but don't know why executing the code gave me such error.
var gith = require('gith').create( 8080 );
var execFile = require('child_process').execFile;
gith({
repo: 'heroku/node-js-sample'
}).on( 'all', function( payload ) {
if( payload.branch === 'master' )
{
//console.log('all',payload );
console.log(__dirname+'/hook.sh');
execFile(__dirname+'/hook.sh', function(error, stdout, stderr) {
// Log success in some manner
if(error) console.log("error",error);
else console.log( 'exec complete',stdout );
});
}
});
You have to make your bash script executable. Use this :
chmod +x hook.sh

spawning a child process to call npm-installed command in nodejs causes ENOENT in windows

I've searched a lot but got no correct answer.
Firstly I'm sure the command is usable under command line, here is the output:
> lessc
lessc: no input files
usage: lessc [option option=parameter ...] <source> [destination]
However when use child_process.spawn, I got:
> node test.js
Encountered error: { [Error: spawn ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'spawn' }
I'm sure the process.env is given to spawn, here is the nodejs code:
var build = require('child_process').spawn(
'lessc',
[],
{
stdio: 'inherit',
env: process.env
}
);
build.on(
'error',
function (err) {
console.log('Encountered error:', err);
process.exit();
}
);
build.on(
'close',
function (err) {
console.log('close');
}
);
And weiredly, it only encounter ENOENT when the command is installed via npm install -g, it works well on for example dir or del system command
As is turns out, the following works:
var spawn = require('child_process').spawn;
var b = spawn(
process.env.comspec,
['/c', 'lessc'],
{ stdio: 'inherit' }
);
Note that you don't need to explicitly pass env as you do, the default is to inherit.

Resources