I'm trying to run instances of console application ( written on cpp ) from nodejs by using child_process .
Here is the code :
function startSingleApp() {
console.log("startSingleApp entered");
var exec = childProcess.exec;
var appOut;
exec("./ConsoleApplication.exe" ,function callback(error, stdout, stderr){
appOut = stdOut;
console.log("started console app");
});
};
And ConsoleApplication4 only prints strings ( cout<<"some string")
Expected behavior : for each instance of ConsoleApplication4 - console window opened and new process created
Actual behavior : no console window , no process create . But "started console app" string printed on console ( from which nodejs app started )
ConsoleApplication.exe located in same folder where nodejs app located
If replace ConsoleApplication.exe by some window app ( notepad or calc) - it works.
The question - what is missing in this code ? Do I need manually catch console out from the consoleapplication ?
Your help will be helpful.
Thaks in advance
If you run a console application from a console, it’s still going to run in the same console. The Windows console is just a convenience that opens automatically when you’re running a console application through Explorer.
You can try it yourself: open cmd.exe first, then run ConsoleApplication.exe. It won’t open a new window; the output will appear beneath your prompt. And then you’ll get another one.
If you want a window to show up, you can run cmd manually:
exec("cmd /c ConsoleApplication.exe", function callback(error, stdout, stderr) {
appOut = stdOut;
console.log("started console app");
});
You can use start to make a new console window like this:
exec("start ConsoleApplication.exe", ....)
and add a line in your C++:
getch();
Related
My Electron application needs to open an external program and wait for it to be closed before reacting again. In Windows this works fine, but macOS has some problems.
So my code for apple is
const cp = require('child_process');
let child;
child = cp.exec('open -a Keynote \"'+pathToFile+'\"'), function(error, stdout, stderr){
if(error) {
throw error;
}
}
child.on('close', function () {
//do something when external application is closed
}
The problem is, that for some reason the process sends the close event immediately after the application is launched. Is there any way to monitor if Keynote is still opened in macOS, or is it just a bug?
Ok. I found out. If anyone stumbles into the same Problem: It needs to be opened with -WParameter. So
cp.exec('open -a Keynote \"'+pathToFile+'\"' +' -W')
does the trick.
Requirement is to fetch the output of a shell script's after running it from the Angular 4 component at the beginning during compilation i.e. just before the website is launched. I have already gone through the threads in stackoverflow i.e. 49700941 and 41637166.
From the first thread i tried to use the below code, but getting error:
Module not found: Error: Can't resolve 'child_process' in 'app/component ...'
const exec = require('child_process').exec; // Can't resolve 'child_process' error coming from this line
exec('/home/myDir/init_setup.sh', (err, stdout, stderr) => {
if (err){
console.error(err);
return;
};
console.log(stdout);
console.log(stderr);
/**
remaining logics
*/
});
Please let me know if I need to import some library explicitly or not to avoid this error.
The modern browsers opens the webpage in isolated sandbox so they have have no access to clients' computers.
Imagine the damage that could be done if a black hat could run batch script on computer that opens his webpage.
The only way to run the script is to run the desktop application on client's machine.
The example code you provided is Node.js code, the desktop framework that user have to install on his machine and run the code intentionally. There's (fortunately!) no way to run it remotely via webpage.
Being New in NodeJS Whenever I run my node JS file in command prompt. I have to close the console window and type the same procedure again. is there any way that i have to not run again and again command prompt.
Use ctrl+c in command prompt and you can use your same command prompt window to run the same program.
For example there are two node.js files.
nodeOne.js
nodeTwo.js
If, these nodes need a port to run, configure both on different port numbers.
e.g. 8080 for nodeOne.js and 8081 for nodeTwo.js
Now, open two instances of 'cmd' and execute both nodes separately.
I'd use "start node whatever.js".
That'll open it up in a separate window, which you can kill whenever and just hit Up arrow on the original cmd window to run the same command.
On the off chance, you're killing the original cmd just to restart node, ctrl-c a couple times should kill it, shouldn't it?
Then up arrow and you've got the last command again.
This could use a little more context, but I assume you are running something like this on the command line:
node my_file.js
Or just:
./my_file.js
Whenever you write command line scripts like this, it's good to call process.exit to tell Node when you're done. So, for example:
#!/usr/bin/env node
function myFunction () {
// Do some stuff that takes a while...
return Promise.resolve()
}
myFunction()
.then(result => {
console.log(result)
process.exit(0)
})
.catch(error => {
console.error(error)
process.exit(1)
})
And if your script is just hanging and not stopping, you can always hit Ctrl + C to abort.
I trying to restart nodejs app which is running in background screen app.
But here i can't do these without reboot my PC. I used forever module but it's start when i reboot my PC but I actually want one button on web-page and when I click on that automatically start node app without restart my PC.
Any One have iDEA about these please let me guide what to Do ?
NOTE : without reboot my system working
You can execute commands from nodejs like this :
nodejs
var child_process = require('child_process');
child_process.exec('forever restart', function callback(error, stdout, stderr) {
// console.log(stdout);
});
I have this in helloworld.js :
console.log('Hello World');
and the output is :
http://img856.imageshack.us/img856/4152/8vh3.png
You're currently using REPL of node.js.
You should run such command directly in CMD.
By the way, you can input raw code, like console.log('Hello') in REPL.
This should be run directly from command line interface as mentioned by #Chichou. If you want to test such things you can also use browser console.