Get full command-line string from child_process.spawn - node.js

I'm spawning a process with child_process.spawn(). How can I view the exact command line that is executed (including all command-line parameters passed into the spawn function)?
Here's my example that isn't working:
var s = require('child_process');
var p = s.spawn('ffmpeg', ['-probesize 1024', '-i "rtsp://192.168.1.10:554/11"', 'test.h264']);
When I capture the stderr, I get a message "Unrecognized option 'probesize 1024'. However, if I run ffmpeg from the command line, it works fine. So I need to see exactly how the command-line options are being mangled before being sent to FFMPEG in order to debug this.
Thanks

Try
var p = s.spawn('ffmpeg', [
'-probesize',
'1024',
'-i',
'rtsp://192.168.1.10:554/11',
'test.h264'
]);
Command line arguments that have a space between them but are coherent, still need to be separated. So there's needs to be a space between -probesize and 1024
Update
If you would like to keep the coherent arguments together, add shell: true to the options object:
var p = s.spawn('ffmpeg',
[
'-probesize 1024',
'-i "rtsp://192.168.1.10:554/11"',
'test.h264'
],
{
shell: true
}
);

Old question I know...but one idea I had, which might answer the OP's question
const cp = require('child_process');
var sp = cp.spawn( 'cmd', [ '/C' , 'echo', 'ffmpeg',
'-probesize',
'1024',
'-i',
'rtsp://192.168.1.10:554/11',
'test.h264'
]);
sp.stdout.on( "data" , (d) => {
console.log( `${d}`);
});
which will echo the cmd to the console...
ffmpeg -probesize 1024 -i rtsp://192.168.1.10:554/11 test.h264

Related

Nested json argument in node child process bash script

I am trying to use node to execute a bash command that takes a nested json object as an argument. I am finding that nested json does not work.
For example trying to convert this bash command. Notice awscloudformation is a nested object.
#!/bin/bash
set -e
IFS='|'
AWSCLOUDFORMATIONCONFIG="{\
\"configLevel\":\"project\",\
\"useProfile\":true,\
\"profileName\":\"testing\"\
}"
PROVIDERS="{\
\"awscloudformation\":$AWSCLOUDFORMATIONCONFIG\
}"
amplify init \
--providers $PROVIDERS \
Here is the node script that does not work as expected. Providers seems to be ignored and is not overriding the defaults like the above bash script.
const arguments = [
'--providers',
`{"awscloudformation":{"configLevel":"project","useProfile":true,"profileName":"testing"}}`,
];
const opts = { stdio: 'inherit', shell: true };
require('child_process').spawn('amplify init', getArgs(), opts);
Would exec be better suited here? It seems I would need to include a variable similar to how $AWSCLOUDFORMATIONCONFIG is being used in the bash script.
It is interesting to note that other arguments that are not nested seem to work fine. For example this amplify argument works fine:
'--amplify',
'{"projectName":"checking987","envName":"dev","defaultEditor":"code"}',
It's better to use shell: false here :
const arguments = [
'--providers',
`{"awscloudformation":{"configLevel":"project","useProfile":true,"profileName":"testing"}}`,
];
const opts = { stdio: 'inherit', shell: false };
require('child_process').spawn("amplify", ["init", ...arguments], opts);
In shell mode, double quotes are removed.
Adapted shell mode version which works :
const arguments = [
'--providers',
`{\\"awscloudformation\\":{\\"configLevel\\":\\"project\\",\\"useProfile\\":true,\\"profileName\\":\\"testing\\"}}`,
];
const opts = { stdio: 'inherit', shell: true };
require('child_process').spawn("amplify init", arguments, opts);

nodejs spawn not constructing valid vim command

If I have this script in node, saved as example.js:
const childProcess = require('child_process');
childProcess.spawn('vim', ['-u NONE', 'test.txt'], { stdio: 'inherit' });
I would expect that node example.js would be (roughly) equivalent to calling:
vim -u NONE test.txt
However, when I execute the script I get:
$ node example.js
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Dec 26 2020 07:37:17)
Garbage after option argument: "-u NONE"
More info with: "vim -h"
Running vim -u NONE example.txt directly works just fine.
Am I misunderstanding / misusing spawn somehow?
Edit:
This, without the -u NONE flag, works just fine:
childProcess.spawn('vim', ['test.txt'], { stdio: 'inherit' });
It's adding -u NONE that for some reason vim doesn't like.
This should work
const cp = spawn('vim', ['test.txt'], {stdio: ['inherit']})
cp.stdout.pipe(process.stdout)
Spawn open a child_process which is attached to the const cp(in this ex), this method stream its output (https://nodejs.org/dist/latest-v14.x/docs/api/child_process.html), so we need to consume it. We pipe the cp.stdout to the parent_process process.stdout. That open a new tty within: the child_process.stdout
We can make it more straightforward by setting the stdio childProcess.output option to 'inherit' and(optional) forwarding the childProcess.stderr to its own childProcess.stdout (which is already inherit by the motherProcess, so will be output automatically).
This should output the same as previously
const cp = spawn(
'vim', ['test.txt'],
{stdio: ['inherit', 'inherit', process.stdout]}
)
To run precisely the command vim -u NONE file.ext. The first argument is the executable path and the second is an array containing the flags to pass to the command. Inside this array each single element(separate with space) of the flags must be an element of the array. So in this case that should work
const cp = spawn(
'vim', ['-u', 'NONE', 'test.txt'],
{stdio: ['inherit', 'inherit', process.stdout]}
)
You could use it like this:
const { exec } = require("child-process");
exec('vim -u NONE example.txt', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});

'Invalid Verb' error only when Calling Inkscape as a subprocess

When I call the following in the command line it works like a charm:
inkscape --with-gui --batch-process --export-filename=- \
--actions="select-all;ObjectToPath" \
/full/path/to/example.svg
But when I open Node.js and make the same call in a subprocess like so:
const cp = require("child_process");
var child = cp.spawn(
"/usr/bin/inkscape",
[
"--with-gui",
"--batch-process",
"--export-filename=-",
'--actions="select-all;ObjectToPath"',
"/full/path/to/example.svg",
],
{
cwd: process.cwd(),
detached: true,
stdio: "inherit",
}
);
I get the following error:
Unable to find: "select-all
verbs_action: Invalid verb: "select-all
Unable to find: ObjectToPath"
verbs_action: Invalid verb: ObjectToPath"
and the file is returned (printed to stdout) unchanged. Any Idea why the verbs are not found when running Inkscape as a subprocess but not calling it directly from the terminal? I get this same error on ubuntu (20.04) and OSX using the latest Inkscape (1.0.1+r73).
When you use cp.spawn with an array of arguments, you don't need to internally quote "select-all;ObjectToPath" like you would with a shell. (In a shell, the quotes prevent the shell from tokenizing the command line into two lines. Due to the same mechanism - or lack thereof - attempting to use shell variables such as $$ or environment variables such as $PATH would fail when you use cp.spawn, since there's nothing to parse that.)
I would imagine
const cp = require("child_process");
var child = cp.spawn(
"/usr/bin/inkscape",
[
"--with-gui",
"--batch-process",
"--export-filename=-",
"--actions=select-all;ObjectToPath",
"/full/path/to/example.svg",
],
{
cwd: process.cwd(),
detached: true,
stdio: "inherit",
},
);
would do the trick for you.

Spawn command with redirection

Let say I have this command
somecli -s < "/path/to/file.txt"
How can I convert the above command to NodeJS spawn command ? I did something like this, but seems like it didn't pass the input.
spawn('somecli', ['-s', '<', '"/path/to/file.txt"'], { stdio: 'inherit'}).on('error', function (error) {
// something
});
I can use the exec command below and it's working, but I prefer if we can see the live output.
exec('somecli -s < "/path/to/file.txt"', (e, stdout, stderr) => {
// something
})
something like this should help
const { spawn } = require('child_process');
const fs = require('fs');
const writeStream = fs.createWriteStream("/path/to/file.txt");
const shell = spawn('somecli', ['-s']);
shell.stdout.pipe(writeStream);
To pass file input to command ( STDIN redirection )
$ somecli -s < /path/to/file.txt
We can do it something like this
spawn('somecli', ['-s'], {stdio: [fs.openSync('/path/to/file.txt', 'r'), process.stdout, process.stderr]});
To pass command output to file ( STDOUT redirection )
$ somecli -s > /path/to/file.txt
You may follow Ashish answer
let s = spawn('somecli', ['-s])
s.stdout.pipe(fs.createWriteStream('/path/to/file.txt'))

Running a shell command from Node.js without buffering output

I'm trying to launch a shell command from Node.js, without redirecting that command's input and output -- just like shelling out to a command using a shell script, or using Ruby's system command. If the child process wants to write to STDOUT, I want that to go straight to the console (or get redirected, if my Node app's output was redirected).
Node doesn't seem to have any straightforward way to do this. It looks like the only way to run another process is with child_process, which always redirects the child process's input and output to pipes. I can write code to accept data from those pipes and write it to my process's STDOUT and STDERR, but if I do that, the APIs force me to sacrifice some flexibility.
I want two features:
Shell syntax. I want to be able to pipe output between commands, or run Windows batch files.
Unlimited output. If I'm shelling out to a compiler and it wants to generate megabytes of compiler warnings, I want them all to scroll across the screen (until the user gets sick of it and hits Ctrl+C).
It looks like Node wants to force me choose between those two features.
If I want an unlimited amount of output, I can use child_process.spawn and then do child.stdout.on('data', function(data) { process.stdout.write(data); }); and the same thing for stderr, and it'll happily pipe data until the cows come home. Unfortunately, spawn doesn't support shell syntax.
If I want shell syntax, I can use child_process.exec. But exec insists on buffering the child process's STDOUT and STDERR for me and giving them to me all at the end, and it limits the size of those buffers (configurable, 200K by default). I can still hook the on('data') events, if I want to see the output as it's generated, but exec will still add the data to its buffers too. When the amount of data exceeds the predefined buffer size, exec will terminate the child process.
(There's also child_process.execFile, which is the worst of both worlds from a flexibility standpoint: no shell syntax, but you still have to cap the amount of output you expect.)
Am I missing something? Is there any way to just shell out to a child process in Node, and not redirect its input and output? Something that supports shell syntax and doesn't crap out after a predefined amount of output, just like is available in shell scripts, Ruby, etc.?
You can inherit stdin/out/error streams via spawn argument so you don't need to pipe them manually:
var spawn = require('child_process').spawn;
spawn('ls', [], { stdio: 'inherit' });
Use shell for shell syntax - for bash it's -c parameter to read script from string:
var spawn = require('child_process').spawn;
var shellSyntaxCommand = 'ls -l | grep test | wc -c';
spawn('sh', ['-c', shellSyntaxCommand], { stdio: 'inherit' });
To summarise:
var spawn = require('child_process').spawn;
function shspawn(command) {
spawn('sh', ['-c', command], { stdio: 'inherit' });
}
shspawn('ls -l | grep test | wc -c');
You can replace exec by spawn and use the shell syntax simply with:
const {spawn} = require ('child_process');
const cmd = 'ls -l | grep test | wc -c';
const p = spawn (cmd, [], {shell: true});
p.stdout.on ('data', (data) => {
console.log (data.toString ());
});
The magic is just {shell: true}.
I haven't used it, but I've seen this library: https://github.com/polotek/procstreams
It you'd do this. The .out() automatically pipes to the process's stdin/out.
var $p = require('procstreams');
$p('cat lines.txt').pipe('wc -l').out();
If doesn't support shell syntax, but that's pretty trivial I think.
var command_str = "cat lines.txt | wc -l";
var cmds = command_str.split(/\s?\|\s?/);
var cmd = $p(cmds.shift());
while(cmds.length) cmd = cmd.pipe(cmds.shift());
cmd
.out()
.on('exit', function() {
// Do whatever
});
There's an example in the node docs for the child_process module:
Example of detaching a long-running process and redirecting its output to a file:
var fs = require('fs'),
spawn = require('child_process').spawn,
out = fs.openSync('./out.log', 'a'),
err = fs.openSync('./out.log', 'a');
var child = spawn('prg', [], {
detached: true,
stdio: [ 'ignore', out, err ]
});
child.unref();

Resources