Command to open terminal from another terminal and run something

Discussion in 'macOS' started by littvay, Mar 22, 2011.

  1. littvay macrumors member

    Joined:
    Mar 1, 2008
    #1
    Dear All

    I am trying to figure out how to open a terminal window from within another terminal window and have the newly opened one run a command. I got this far:

    open -na Terminal.app

    This opens the new terminal Window (actually, probably opens another instance of Terminal which is not too efficient, but works). But how do I augment the command so the new window automatically runs something, say: ls -al? (Need to be able to supply arguments to the command I will run.)

    A colleague got it to work running it from xterm. But it is clearly a hack. Any ideas how to do it better? Am I going about this the wrong way entirely?

    Thanks

    L
     
  2. larkost macrumors 6502a

    Joined:
    Oct 13, 2007
    #2
    Rather than ask specifically about methods, tell us what you are really trying to achieve.
     
  3. littvay thread starter macrumors member

    Joined:
    Mar 1, 2008
    #3
    In all honesty, I am asking for a colleague who asked me. They have a shell based executable software that does specialized statistics. They would need to be able to do what I described above with this shell based specialized software. (So substitute ls -al for the executable of this software and operands.) They want to do this because their executable currently runs on a single CPU thread but there are some repetitive procedures that could be sped up if they could run the various repetitions in multiple threads.

    In the Linux version (thinking DOS as well, but not sure about that) it was solved by opening another terminal and running the the repetition there. It seemed to work fine. I was beta testing the Linux version for them. But for some reason they cannot seem to find the shell command to do the same in a mac. They know I am a mac user so I asked. I could not figure it out so I posted the question.

    Does this help? Is this what you were thinking in terms of more specifics?

    Thanks.

    L
     
  4. deboni macrumors member

    deboni

    Joined:
    Jun 9, 2007
    Location:
    Oakland, CA
    #4
    The "xterm" solution is not a hack; it's a legitimate way to accomplish what you're trying to do. You can find out more about this by typing "man xterm" in an open terminal window. But to cut to the chase, one command line that will work for you is "xterm -e program arguments". If you append an "&" character on the end, the "program" will run in the background, and control will be returned to you immediately; this can be useful if you want to get several of these things running simultaneously.

    What you didn't tell us is whether you want the command and the second terminal window to run on the same machine as the first terminal, or a different machine. The example above will run on the same machine.

    The way to do such a thing on a different machine is with an ssh command.
    You'll want to run "man ssh" to see the details, but something like
    "ssh user@hostname program" will achieve the desired results.

    This stuff is pretty ordinary within Unix/Linux, and once you've gotten the commands worked out to your satisfaction - including niceties such as redirecting input and output files - you can either alias it all to a single command name, like "getitdone", or put a bunch of commands into a file, make it executable, and invoke it via the file's name; so you need to type only the alias or file name to make it all happen.

    Unix is very flexible, and once you get used to it, you can become very efficient in using it. This is something the many OS X users don't appreciate.

    Good luck!
    Tom

     
  5. deboni macrumors member

    deboni

    Joined:
    Jun 9, 2007
    Location:
    Oakland, CA
    #5
    One more thing...

    I neglected to mention that you really don't need to open a terminal window to get a program to run. You need only to invoke it, and doing so several times in sequence, with "&" ending each invocation, will get each one running in the background, and allow them all to start up one after the other. If there are thread/execution resources available (i.e., an available cpu or core) for each invocation, you will get the benefit of the parallelism.

    And this is true even if you're running multiple programs i parallel on multiple machines.

    Tom
     
  6. littvay thread starter macrumors member

    Joined:
    Mar 1, 2008
    #6
    Thanks for the insight. I want to run all this on the same machine using the available CPU threads. Running things in the background would not be appropriate here as the executable gives some feedback of progress that can be useful. So it is good it see it.

    I have two concerns with doing this in xterm. Last I checked (might have been back in the OSX 10.3 days) X is not installed by default. Or did that change? On the other hand the Terminal.app is available for sure. Also, doesn't xterm also run... X? I assume this uses a lot of unnecessary when all I want to run is an executable (written in Fortran) that takes a text input and grabs a text output. My assumption could be wrong and xterm is using extra resources unless something "graphical" starts up.

    As for Unix. You are totally right. After Windows ME pissed me off sufficiently I decided no more. I switched to Debian and used it for about five years. While I am consider THE computer whiz among family and friends I never even approached super user status by Linux user standards. When Ubuntu's unstable branch started living up to its name I bought a Mac (Refurbished Powerbook 12" 867MHz). It was relatively cheap so I figured if I don't like it, I can sell it. I would have never made the move if Macs didn't have Unix/Terminal capabilities. My heart has been with the Mac since. Unfortunately, I had to switch back to Windows for a year or specifically because of statistical tools that were not available otherwise. Ironically the one I am helping to troubleshoot now was one of these. Back then it was Windows (or dos) only. Now the Linux version seems to be running awesome and they moved on getting the mac version running as well. (I am very happy about this.) When Intel macs, bootcamp and parallels came out I switched back to a mac. (Hate Windows with a passion. Nothing changed.) Now work keeps a quad-core, lots of RAM Windows "server" I can log into and use for my statistical needs. And I am a very happy Macbook Air user :)
     

Share This Page