I have an application (closed-source tool not developed by me) which requires another process to exist before executing properly. When I quit this application, I also want that custom process to be killed. My solution is to have a shell script "launcher". I can handle creating shell script menus, the on-close event, and opening/killing the processes. The problem is that I can't seem to figure out how to open an application from Terminal but keep Terminal locked on that application so that when I close the Terminal window or send CTRL+C the application closes. If you're familiar with Linux, a simple example of my desired behavior is just "gedit file.txt". The text-editor would open but it only lives as long as the terminal window does.

On OSX with the "open" command, I can start applications but they aren't linked to the Terminal window. Is there an alternative command I should be using to achieve this behavior?

Here is my script so far:

#!/bin/bash
set -e
function cleanup {
    osascript -e 'tell application "APP1" to quit'
    osascript -e 'tell application "APP2" to quit'
}
trap cleanup EXIT

clear

PS3='Please select an option: '
options=("Start Apps" "Stop Apps")
select opt in "${options[@]}"
do
    case $opt in
        "Start Apps")
            open -a "APP1"
            open -a "APP2"
            ;;
        "Stop Apps")
            cleanup 
            ;;
        *) echo invalid option;;
    esac
done
  • Linux and OS X work the same - the issue here is using open which opens in a non sub process - call the command directly as the Unix executable - However this is not the best way either your first app should fork the background one and then killing the terminal fills the first app which kills the background one as they are sub processes or make the background app a daemon – Mark Mar 17 '15 at 14:40
  • Rather than use open. You can execute the executable file of the applications. /Applications/TextEdit.app/Contents/MacOS/TextEdit – markhunte Mar 17 '15 at 14:58
  • Thanks, guys! @markhunte make that an answer and I'll accept it! That was exactly what I needed. – CauselessEffect Mar 17 '15 at 16:34
  • Ok. Answer added – markhunte Mar 17 '15 at 17:13
up vote 0 down vote accepted

Rather than use the Unix command open.

You can execute the executable file of the applications directly.

For example for TextEdit.app

Running the command in Terminal.app

/Applications/TextEdit.app/Contents/MacOS/TextEdit

This will open TextEdit using a process from Terminal.app

When you close the terminal window or use Ctrl + C

TextEdit will quit.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.