How to detach child_process from a node.js one? - node.js

I'd like to execute a shell command starting a long-running script from node and have the node process exit. For example, this keeps the node process running:
var exec = require('child_process').exec;
exec('gedit &', function() {
});
While I'd like it to exit just after gedit starts.

You can always exit hard by putting process.exit(0) right after the exec call.

Related

Calling processing-java from nodejs on Windows 10

I am having some problems to execute the processing-java on windows from nodejs. When I use the following
processing-java --sketch=$pwd/sketch/ --run
in the console it works fine and the sketch is executed, window opens and the result is a file written to a directory where I need it.
But when I use the same as a string in the node file to execute it, it does not open a window and no file has been written, but the callback function processOutput is executed, hence the somehow the execution happened.
I added
const exec = require('child_process').exec;
to the js file which is used for executing child processes in node.
$pwd is the PowerShell equivalent to pwd on MacOS.
exec should execute the shell command and call the callback function. The callback function is executed, but not the sketch.
const cmd = 'processing-java --sketch=$pwd/sketch/ --run';
exec(cmd, processOutput);
What am I missing? Any help appreceated.
Many thanks, C.
child_process.exec() does not execute in a powershell but in a CMD terminal. Thus you can't use powershell variables like $pwd. You could try using %CD% instead
const cmd = 'processing-java --sketch=%CD%/sketch/ --run';
exec(cmd, processOutput);

Need to call function written inside a shell script from node js

I need to call a function written inside a shell script from node js. I have tried the command "source test.sh a" , where 'a' is the method inside test.sh in shell command. It works but from nodejs how to call it using execFile/spawn.
With child_process spawn, you could do it with the { shell: true } option (to spawn a shell), but it seems that the exec method is more appropriate in this case:
const { exec } = require('child_process');
exec('source ./test.sh && a');

Multi-command shell AS detached child process WITH spawn() IN Node.js

For reference: https://nodejs.org/api/child_process.html#child_process_options_detached
Hey guys,
So I need to spawn a child child-process, spawn because exec doesn't allow a options.detached & child.unref();, meaning it can be decoupled with the parent allowing the child to run and finish on it's own, vice-versa also the parent (in our particular case, the parent process can die before a long-running child, update in that case, is finished without needing to wait for the child like with exec).
We have a long connected ("… ; … ; …") command that is built by the node (parent) app, but as like spawn("echo stuff >>stderr.log") doesn't work, only spawn('ls', [-l]), I obviously can't chain commands (as it's also referenced in he docu and multiple times on SO.
TLDR;
We need to use spawn, but spawn can't process chained shell commands.
Do I really now need to write my command in a bash and execute that then, is this REALLY the ONLY option??
THX
Notice the shell option for spawn:
If true, runs command inside of a shell. Uses '/bin/sh' on UNIX, and 'cmd.exe' on Windows. A different shell can be specified as a string. The shell should understand the -c switch on UNIX, or /d /s /c on Windows. Defaults to false (no shell).
So:
let child = child_process.spawn('foo; bar; blah', { shell : true });
EDIT: if you're using a Node version that doesn't support this option, here's an alternative:
let child = child_process.spawn('/bin/sh', [ '-c', 'foo; bar; blah' ]);

nodejs spawn a process in real shell and kill this process

I'have a process created with a spawn in nodejs with the option shell:true so the process starts in a real shell. So when I try to kill this process with streamingTask.kill() it's not working. Without the option shell:true everything works fine.
This is how my code looks:
var options = {shell:true};
streamingTask = spawn('gst-launch-1.0',args,options);
...
streamingTask.kill()
So how can I kill this process now?
This doesn't work because you are killing the shell process itself, not the child process(es) spawned by the shell (gst-launch-1.0 in your case).
There's a package on npm called tree-kill that provides an easy one-line solution:
var kill = require('tree-kill');
kill(streamingTask.pid);

Simple bash commands fails, cannot execute binary file

I try to execute some simple bash in node bu get an error:
/usr/bin/file: /usr/bin/file: cannot execute binary file
'use strict';
var spawn = require('child_process').spawn;
var process = spawn('bash', ['file']);
process.stdout.on('data', function(data){
console.log(data.toString());
});
process.stderr.on('data', function(data){
console.log(data.toString());
});
I even gave the script chmod+x and i run it with node script.js
Any ideas?
It looks like when you're spawning the child process, you're actually trying to run the "file" command, not a shell script, so bash is barking at you.
It would be equivalent to typing this on the command line: "bash file".
You'll want to write a shell script and pass that as the parameter to the bash process.
So, write a script called "do_something.sh" and then run your code with ['do_something.sh'] as the parameter to bash rather than ['file']:
var process = spawn('bash', ['do_something.sh']);

Resources