Run Bash-Script within Electron App using child_process.exec - node.js

I'm struggling with running a bash-script within main.html.
const exec = require("child_process").exec;
// Execute bash script
exec("/c/workspace/_edu_zone/Proxy_Manager/filemover.sh", shellCallback);
// Callback
function shellCallback(error, stdout, stderr) {
console.log(error, stdout)
}
I'm always getting the error: no such file or directory. What am i doing wrong?
Any help is highly appreciated.

change
/c/workspace/_edu_zone/Proxy_Manager/filemover.sh
to
c:/workspace/_edu_zone/Proxy_Manager/filemover.sh
or
your could try using node-powershell to execute the command directly
const shell = require('node-powershell')
let ps = new shell({
executionPolicy: 'Bypass',
noProfile: true
});
function lunchnode() {
process.stdout.write('logging');
ps.addCommand('node run.js')
ps.invoke()
.then(function (output) {
process.stdout.write(output)
}).catch(function (err) {
process.stdout.write(err)
ps.dispose()
})
}

Related

How to handle mysql command-line password prompt using exec function

I don't know how id pipe the password variable in the exec function.
I've tried running it, but no prompt appears.
const {user,password,database} = require('./config.js');
const { exec } = require('child_process');
const comm = `mysql -u ${user} -p ${database} < ${QUERY_PATH} `
exec(comm)
I guess the only part missing is to use the callback, as recommended by Node in https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback .
Replace:
exec(comm)
by:
exec(comm, (error, stdout, stderr) => {
console.log(stdout);
})
This way, you need to type the password manually. If you don't want to type the password manually (not recommended, mySQL warns about safety concerns using this second method), then the code would be:
const comm = `mysql -u${user} -p${password} ${database} < ${QUERY_PATH}`
If you want to check what is the error happening to you, and that's how I debugged, use the callback function to log the errors:
exec(comm, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});

Stop node server from another file

I have the following two files.
file1.js and file2.js
From file1.js, I start a node server that exists in files2.js by using Node exec from child process.
What I would like to do is, start the server that exists file2.js from file1.js by calling exec or spawn method from child_process. Stop it after 10s or so and restart the server again. How could I achieve this?
What I've tried is that, after I started the server, I called process.exit() and then tried to execute the exec function again, but since the process has exited, the second call to exec never actually reached.
I want to achieve this without using any external package though.
One way to do this is setTimeout():
const { exec } = require('child_process');
let stopped = false;
exec('command to start the server in file two', (err, stdout, stderr) => {
if (err) {
// handle error
} else {
// do stuff
}
});
setTimeout(function(){
exec('stop the server in file two', (err, stdout, stderr)=>{
stopped = true;
if(err){
// handle error
} else {
// do stuff
}
})
}, 10000);
if(stopped) {
exec('start server again', (err, stdout, stderr)=>{
stopped = false;
if(err){
// handle error
} else {
// do stuff
}
})
}

child_process.exec not finding cmd in path

So I have installed ffmpeg and added it into my path.
If I run ffmpeg in cmd.exe, it runs ffmpeg.exe.
But when I use this:
const { exec } = require('child_process');
await asyncExec(`ffmpeg -i ${filename}%05d.png ${aviname}`);
//Note: the variables are filled in
I receive this error:
Command failed: ffmpeg -i thing%05d.png thing.avi
'ffmpeg' is not recognized as an internal or external command,\r\noperable program or batch file.
I do not understand why this happpens. I'm just running this app in VSCode debugging.
Edit: asyncExec is this function:
const asyncExec = text => new Promise((res, rej) => {
exec(text, (err, result) => {
if (err) {
rej(err);
} else {
res(result);
}
});
});
After restarting Visual Studio Code it works again. Weird.

NodeJS exec() command for both Windows and Ubuntu

Using NodeJS, NPM, and Gulp.
I want to build a gulp task to run JSDoc that works on Ubuntu and Windows.
This works on Ubuntu...
var exec = require('child_process').exec;
return function(cb) {
exec('node node_modules/.bin/jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
cb(err);
});
};
And this works on Windows...
var exec = require('child_process').exec;
return function(cb) {
exec('node_modules\\.bin\\jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
cb(err);
});
};
Needless to say, neither works on the other. How do others solve this type of problem?
Try using path.resolve, which should provide you with a full path to the file regardless of the platform.
Node has process.platform, which... "returns a string identifying the operating system platform on which the Node.js process is running. For instance darwin, freebsd, linux, sunos or win32"
https://nodejs.org/api/process.html#process_process_platform
var exec = require('child_process').exec;
return function(cb) {
if (process.platform === 'win32') {
// Windows OS
} else {
// everything else
}
};
Using path.resolve:
const exec = require('child_process').exec;
const path = require('path');
return function(cb) {
let command = `node ${path.resolve('node_modules/.bin/jsdoc')} -c jsdoc-conf.json`;
exec(command, function(err, stdout, stderr) {
cb(err);
});
};

(Node.js) How to store stdout.pipe into a variable?

I want to get free -m (linux shell command) and store its result into a variable
by using source code below:
var spawn = require('child_process').spawn,
command = spawn('free', ['-m']);
command.stdout.pipe(process.stdout);
Is there any way to store process.stdout in a variable, please me some suggestions
This is fairly simple with child_process.exec:
var child = require("child_process");
var freeOut;
child.exec("free", ["-m"], function (error, stdout, stderr) {
if (error) {
console.error(error, stderr);
return;
}
//stdout and stderr are available here
freeOut = stdout;
process.stdout.write(stdout);
});
//Note. Do NOT use freeOut here. exec is async. Can only use in the callback

Resources