Using node.js and coffeescript for executing Ubuntu commands - node.js

I am using child_process.exec to execute Ubuntu commands with node.js in coffeescript. When I execute the following commands:
list = child_process.exec("ls")
print list
It prints this:
[Object Object]
Why isn't a proper output of ls command printed? What should I do to get a proper output for commands?

You're attempting to run an asynchronous function synchronously. The correct way to do this is:
var exec = require('child_process').exec;
exec('ls', function (error, stdout, stderr) {
console.log(stdout);
});
Source: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
If you really wish to execute a command synchronously, you can use execSync. However, I'd advise against that, since it blocks your node code from doing anything until the process finishes.
ExecSync: https://nodejs.org/api/child_process.html#child_process_child_process_execsync_command_options

Found it!
Can be accessed using ->
print list.main.<attribute_name>

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);

Execute 'mv' command common in Bash, Zsh, PowerShell, CMD

Im trying to create CLI using node.js and I have some tasks where I need to move a folder from one place to another. So to achieve this I used node.js's child_process. Like this:
const { exec } = require("child_process");
function moveFolder(folder,destination){
exec(
`mv ${folder} ${destination}`,
(error, stdout, stderr) => {
if (error) {
console.log("Move error");
}
console.log("Move success");
}
);
}
This piece of code works when I execute this with bash terminal (git-bash or in Linux). But when I execute it using CMD or PowerShell it throws an error
'mv' is not recognized as an internal or external command.
I know PowerShell and CMD have a command called move to achieve this, and that's why it is throwing the error. But is there a way (library or some other method) to execute these types of commands in all environments? (in Windows (CMD, PowerShell), Linux (bash, zsh), Mac OS (zsh)). How can I achieve this?. Can we detect the shell type and execute different commands?

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']);

Launch node script from bash script

I've my program in bash and I want to launch a node program to get the string that it return, like in this way:
#!/bin/bash
mystring=$( node getString.js)
mplayer $mystring
Googling I found that I should inlcude
#!/usr/bin/env node
But I need to use string to give it to mplayer.. any ideas?
Solution
As Zac suggesting (and thanks to this link) I solved my problem in this way:
script.sh
#!/bin/bash
mplayer ${1}
script.js
/* do whatever you need */
var output="string"
var sys = require('sys');
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout); }
exec("./script.sh " + output, puts);
Consider simply writing an executable Node script (with the #!/bin/env node line), and, instead of using Bash, just use Node to run the external UNIX command. You can use the child_process module for this, as illustrated in this example. This question is also helpful when debugging shell-style subcommands in Node scripts.
If your example really is all you need to do in Bash, this should be sufficient. The #!/bin/env node line allows your script, once marked as executable, to run as its own program, without having to be invoked with node.

Capture node.js crash reason

I have a script written in node.js, it uses 'net' library and communicates with distant service over tcp. This script is started using 'node script.js >> log.txt' command and everything in that script that is logged using console.log() function gets written to log.txt but sometimes script dies and I cannot find a reason and nothing gets logged in log.txt around the time script crashed.
How can I capture crash reason?
Couldn't you listen to uncaughtException event. Something along the lines of =>
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
P.S: after that you are adviced to restart process according to this article from Felix Geisendörfer
It's much easier to capture exceptions by splitting stdout and stderr. Like so:
node script.js 1> log.out 2> err.out
By default, node logs normal output to stdout, which I believe you are capturing with >>.
As noted in the comments, a segmentation fault is a signal put to stderr by the shell, not necessarily your program. See this unix.stackexchange answer for other options.

Resources