Run NodeJS bat file and finish the process - node.js

I want to run a bat file with child_process module in NodeJS but I want the application to stop and the bat file to continue processing without waiting for the result of the application.
Code I used:
const { exec } = require("child_process");
exec("start cmd /c my.bat");

Related

EBUSY error when downloading and executing a .exe file with Node.js

I'm trying to download an executable file with Node.js and execute it as soon as the file is downloaded. I should launch it using the spawn method because I want the new process to be detatched. This is what I have done:
var file = fs.createWriteStream("myex.exe")
http.get("http://host/myex.exe", function(response) {
response.pipe(file);
const subproc = spawn('myex.',['param1', 'param2'], {
detached: true,
stdio: 'ignore'
});
subproc.unref();
});
The problem is that when node.js tries to launch the executable in a new process, an exeption arises:
Error: spawn EBUSY
I tried to execute in the same way the .exe file without downloading it and the process is correctly created. If I execute it with the exec function it works perfectly but I cannot detach the new process. What is the problem and what could be a possible solution?
i think the problem is file is occupied, my way is , use settimeout ,wait for a while , and then use execFile

Nodejs child_process exec not running /bin/bash

I am trying to run a bash instance from node js but nothing seems to happen when I run the following code:
const { execSync } = require("child_process");
execSync("/bin/bash");
Then when I run ps to see if there is another bash process running there is no extra bash process running.

Node.js as service, exec doesn't work

I'm running Node.js project as service using nssm. When user clicks button on my nodejs website it should run
require('child_process').exec('cmd /c batfile.bat', function({ res.send(somedata); });
but instead it just skips running bat file and jumps to res.send(somedata). Why is that?
When I run Node.js using cmd and npm start server.js it works fine. How can I make exec work while running nodejs as service?
Edit, some code:
require('child_process').exec('cmd /c batfile.bat', function(){
var log = fs.readFileSync('logs/batlog.log', 'utf8');
var html = fs.readFileSync('logs/batlog.htm', 'utf8');
var json = {"log": log, "html": html};
res.send(json);
});
and the batfile.bat is supposed to generate those 2 files but it just doesn't if I run nodejs as service.

node.js child process change a directory and run the process

I try to run external application in node.js with child process like the following
var cp = require("child_process");
cp.exec("cd "+path+" && ./run.sh",function(error,stdout,stderr){
})
However when I try to run it stuck, without entering the callback
run.sh starts a server, when I execute it with cp.exec I expect it run asynchronously, such that my application doesn't wait until server termination. In callback I want to work with server.
Please help me to solve this.
cp.exec get the working directory in parameter options
http://nodejs.org/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback
Use
var cp = require("child_process");
cp.exec("./run.sh", {cwd: path}, function(error,stdout,stderr){
});
for running script in the "path" directory.
The quotes are interpreted by the shell, you cannot see them if you just look at ps output.

NodeJS: exit parent, leave child alive

i am writing an utility. One command of this utility is to run an external application.
var child_process = require('child_process');
var fs = require('fs');
var out = fs.openSync('.../../log/out.log', 'a');
var err = fs.openSync('.../../log/err.log', 'a');
exports.Unref = function(app, argv) {
var child = child_process.spawn(app, argv, {
detached: true,
stdio: [ 'ignore', out, err ]
});
child.unref();
//process.exit(0);
};
Currently:
$ utility run app --some-args // run external app
// cant enter next command while app is running
My Problem is that if i run this command, the terminal is locked while the "external" Application is running.
But the terminal window shouldn't be locked by the child_process.
i wanna run:
$ utility run app --some-args
$ next-command
$ next-command
The external Application (a desktop application) will be closed by hisself.
Like this:
$ subl server.js // this runs Sublime Text and passes a file to the editor
$ npm start // The terminal does not locked - i can execute next command while Sublime is still running
You know what i mean ^^?
Appending ['>>../../log/out.log', '2>>../../log/err.log'] to the end of argv instead of leaving two files open should work since it's the open file handles that are keeping the process alive.
Passing opened file descriptors in stdio in addition to detached: true will not work the way you expect because there is no way to unref() the file descriptors in the parent process and have it still work for the child process. Even if there was a way, I believe that when the parent process exited, the OS would clean up (close) the file descriptors it had open, which would cause problems for the detached child process.
The only possible way that this might have been able to work would have been by passing file descriptors to child processes, but that functionality was dropped several stable branches ago because the same functionality did not exist on some other platforms (read: Windows).

Resources