0

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

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • Why do you believe `terminal` should run the default terminal? Does it work in any other context? – that other guy Jan 06 '16 at 21:01
  • 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 Jan 06 '16 at 21:03
  • in applications/utilities on my Mac, I have terminal.app, I guess I could run that – Alexander Mills Jan 06 '16 at 21:07
  • I'm confused about your question. You mention OSX, but you've tagged your question [tag:linux]. What part of this is about Linux? – ghoti Jan 06 '16 at 21:12
  • the bash part is about linux – Alexander Mills Jan 06 '16 at 21:12
  • I want this to work on both osx and linux – Alexander Mills Jan 06 '16 at 21:13
  • 1
    Also, this looks VERY much like an [XY Problem](http://mywiki.wooledge.org/XyProblem). 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](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/screen.1.html) or tmux? Or simply redirecting your stdout/stderr to a log file? – ghoti Jan 06 '16 at 21:15
  • Why is the bash part about Linux? And where in your question does it say that? You're using bash in OSX, I use bash in FreeBSD. There is nothing inherently related to the Linux kernel simply because you're using bash as your shell. – ghoti Jan 06 '16 at 21:16
  • well in theory the bash commands might be the same, right? – Alexander Mills Jan 06 '16 at 21:24
  • Do you need it in a new window, or could you write it to the window your existing node process is running in? See here for an example of piping it to your existing window: http://stackoverflow.com/a/15515636/78496 – chedabob Jan 06 '16 at 21:53
  • thanks, yeah I would prefer it to be a new window, but I could give the user of my library the option of using same window or no window at all – Alexander Mills Jan 06 '16 at 21:54

3 Answers3

4

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.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • this is working for me "open -b com.apple.terminal", do you know what the -a vs -b options are for? – Alexander Mills Jan 06 '16 at 21:20
  • 1
    `-a` specifies the application to use for opening the file, `-b` specifies the bundle identifier for the application to use when opening the file. They are essentially just two different ways of specifying the application to open the file. – l'L'l Jan 06 '16 at 21:23
  • yeah the first part works (open -a Terminal.app) but it doesn't seem to execute the script. The script works on its own, so somethings weird, but thanks, this helps – Alexander Mills Jan 06 '16 at 21:33
  • Maybe try doing a chmod on your script if it's not executing: (eg. `chmod +x script.sh`). Also in OS X usually my script header is like: `#!/bin/bash` – l'L'l Jan 06 '16 at 21:37
  • this sort of works, but I posted an answer to my own question that seems to work better – Alexander Mills Jan 06 '16 at 21:55
3

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

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

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • yeah probably not, if you can advise as how to create a quick and dirty solution on linux that would help me – Alexander Mills Jan 06 '16 at 22:10
  • 1
    Depends on your desktop environment. As someone else suggested, `gnome-terminal` launches a terminal if you're using Gnome. You might try `gnome-open` as a drop-in replacement for OSX's `open`. But these won't work if your Linux desktop environment is KDE or Afterstep or a tiling window manager. If you want something that is actually platform independent, you are likely out of luck unless you go text-only and use GNU Screen. – ghoti Jan 06 '16 at 22:22