0
votes

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

3
Why do you believe terminal should run the default terminal? Does it work in any other context? - that other guy
Maybe I am confusing windows with OSX, but that was my impression. But now "terminal" is not recognized at the command line. I just want to open any terminal window, whether it's the default bash terminal on OSX or iterm2/iterm3 - Alexander Mills
in applications/utilities on my Mac, I have terminal.app, I guess I could run that - Alexander Mills
I'm confused about your question. You mention OSX, but you've tagged your question linux. What part of this is about Linux? - ghoti
Also, this looks VERY much like an XY Problem. You're asking us to help you implement a solution which almost certainly is not the best way to solve your underlying problem. Have you considered using GNU Screen or tmux? Or simply redirecting your stdout/stderr to a log file? - ghoti

3 Answers

4
votes

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.

3
votes

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.

0
votes

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