I want to open a bash shell from nodejs console.
I have tried everything from child_process module, but i did not managed to find a solution where i can see $ prompt.
Thanks
You can make the child inherit the stdio, here you have a working example (Linux) :
const { spawn } = require('child_process')
const shell = spawn('sh',[], { stdio: 'inherit' })
shell.on('close',(code)=>{console.log('[shell] terminated :',code)})
example output :
sh-4.4$ uname
Linux
sh-4.4$ exit
exit
[shell] terminated : 0
Remember to replace sh with the correct shell for your platform requirements
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
Question: Can I access $variable via {spawn} = require("child_process") ?
Background: I have already tried using exec("echo $HOME"); which output the correct value.
But when I tried to use spawn("echo",["$HOME"]), I got $HOME
And how to define the variable by using spawn? Currently I'm setting the child_process option {env:{data:1000}}, is this correct or not ?
by default, spawn does not create a shell and then execute the command within that shell (as appose to exec which starts a shell first and then executes the command within that shell).
Since there is no shell, the "$HOME" environment variable is not set. You can set spawn to execute the command within a shell:
spawn("echo",["$HOME", { shell: true });
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 trying to write a Node.js script that will start a Node.js server in a new process, in a new command window.
I believe I am close. I have this:
var n = cp.spawn('sh', [ 'start-server.sh' ]);
the contents of start-server.sh are like so
#!/usr/bin/env bash
node bin/www
this starts the server successfully, but it doesn't open a new terminal window, so I can't see any of the stdio of the spawned process.
So I am thinking that I should open a new terminal window in the bash script and then run the node command, so then the bash script contents would become
#!/usr/bin/env bash
terminal -e "node bin/www"
the problem is that "terminal" is not recognized at the command line. Why is that? I believe the "terminal" command should default to whatever program is being used as your default terminal application.
Please advise if this is the best way to do this and why "terminal" might not be recognized at the command line in OSX, thanks!
this is what is in my path
echo $PATH
/Users/amills001c/.rvm/gems/ruby-2.2.1/bin:/Users/amills001c/.rvm/gems/ruby-2.2.1#global/bin:/Users/amills001c/.rvm/rubies/ruby-2.2.1/bin:/Users/amills001c/google_app_engine_stuff/google-cloud-sdk/bin:/usr/local/bin:/usr/local/bin/meteor:/usr/local/redis/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Users/amills001c/golang/bin:/Users/amills001c/apache-maven-3.3.3/bin:/Users/amills001c/.rvm/bin
In OS X you would normally run the command like so:
open -a Terminal.app /path/to/script.sh
Which would open a new terminal window and execute your script.
Check the real name of the "terminal" command in your system, to check it, for example, in Ubuntu do "/usr/bin/env | grep terminal", in my case is "gnome-terminal", then use "gnome-terminal -e XXX" should work. Hope it helps J.
What worked for me was this:
var cp = require('child_process');
cp.spawn('sh', [ 'start-server.sh' ], {
detached: true,
env: {
NODE_ENV: process.env.NODE_ENV
}
});
#!/usr/bin/env bash (start-server.sh)
open "./run-node.sh"
#!/usr/bin/env bash (run-node.sh)
node ./bin/www.js
the open command will open the .sh file with the default app, which happens to be iterm2 for me, not terminal.app
the next problem I have is I have to pass paths as arguments to .sh files and I don't know how to do that yet
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']);