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']);
Related
The following snippet of code opens Git bash and runs the shell commands in Windows. But in MacOS, it doesn't open the terminal for the same. Shell script is not being run.
const exec = require('child_process').exec;
var str = "test.sh"
const myShellScript = exec(str);
myShellScript.stdout.on('data', (data)=>{
console.log(data);
});
myShellScript.stderr.on('data', (data)=>{
console.error(data);
});
It was a silly mistake. To execute, the syntax is "sh filename.sh". Problem is solved.
Click (https://medium.com/stackfame/how-to-run-shell-script-file-or-command-using-nodejs-b9f2455cb6b7) to know more
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');
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>
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.
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.