2

I'd like to run a bash file on a darwin via:

open -n -a Terminal --args bashfile.sh

The bash file itself looks like:

#! /bin/sh
<application> <arguments>

It works as expected, but is there a way to close the terminal window when the process is done?

I already tried exit; and exit 0; but without success.


Partial solution

In the Terminal's preferences (Terminal > Preferences > Settings > Shell) you can set the 'When the shell exists' flag to close the window if the shell exited cleanly. If nothing went wrong, this closes the terminal window afterwards, but not the application as such.

2

If this is the only one terminal window, then you can try killing its process from script, which will terminate Terminal process and bash script running in it. The hardest part here is to determine right way to kill it that will work for you. The easiest way is to do:

killall Terminal

I guess multiple Terminal windows run in single process, so that means that you'll have all windows closed after this command.

If you are using other terminal emulator as your main tool (like iTerm), then it won't be a problem.

  • Thanks Pavel! But unfortunately this closes all terminal windows. – p2or Aug 7 '16 at 13:55
1

You tag but shebang points to /bin/sh. I assume sh is symlinked to bash in your system.

Try

kill $PPID

at the end of your script. As $PPID expands to the process ID of the shell parent, it should kill the right Terminal and leave other instances alone.

On my Ubuntu Terminal is GNUstep terminal emulator. I don't know how similar it is to yours.

Furthermore my Terminal (the package terminal.app, version 0.9.8-1+nmu1build1, OS Ubuntu 14.04.5 LTS) often misbehaves when it receives the signal from within the script; it hangs hopelessly. A bug, I guess. The only firm way to kill it is with force:

kill -SIGKILL $PPID
  • I think it's a specific osx thing. I'm sure that this will work on linux, but here something seems to block the kill command. The terminal window closes as expected, but the application is still running. Anyway, thanks Kamil! – p2or Aug 9 '16 at 7:05
0

The Bash man page has a section on Special Parameters. The one you want is $$, which expands to the PID of the current shell.

kill $$ should do what you want, though I'm on Win7 now and can't test it.

  • It returns "Terminated", but leaves the window open. Thanks anyway. – p2or Aug 7 '16 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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