3

I have to set up a bash script which will be in the autorun for all our users.

I can not set the terminal settings to close on "process complete". I can not 'KillAll' the terminals.

Does someone know an approach? I think the PID-solution (not parent pid) would be a nice way.

OS used is MacOS Sierra.

test.sh

#!bin/bash
currentUser=$(whoami)
mkdir -p ~/Desktop/Pdrive
mount_smbfs //server/UserData/$currentUser ~/Desktop/Pdrive


#pid=$$
#kill $pid
#exit 0
#kill -15 $$
#disown
#kill -9 $(ps -p $(ps -p $PPID -o ppid=) -o ppid=)
#osascript -e 'tell application "Terminal.app" to quit'
#pkill -f test.sh
exit
5
  • You are trying to kill all terminal sessions?
    – Luka
    Mar 16, 2017 at 13:47
  • 1
    Try this: osascript -e 'tell application "Terminal" to close first window' #to ask you before leaving osascript -e 'tell application "Terminal" to close first window' & exit #leave without asking. P.S Please do not use killall because killall does different things on different Unix versions.
    – Luka
    Mar 16, 2017 at 13:50
  • 1
    Thanks alot Luka, this is exactly what I was searching for
    – Sevi
    Mar 16, 2017 at 14:26
  • Luka: It would be best if you made your comment into an answer, so that the asker can mark it accepted; this will help future users find it.
    – dhag
    Mar 16, 2017 at 14:52
  • OK I did, now asker can accept it.
    – Luka
    Mar 16, 2017 at 16:29

3 Answers 3

1

Try this:

If you wish to be asked before leaving:

osascript -e 'tell application "Terminal" to close first window' 

Leave without asking:

osascript -e 'tell application "Terminal" to close first window' & exit

Please do not use killall because killall does different things on different Unix versions.

0

You could run your script in the current shell using source (also known as .):

. ./test.sh

This way, when you script calls exit, your shell will exit too. This should cause your terminal emulator to close as well.

This does not, however, work with Terminal from macOS with its default settings, but you could go to Preferences → Profiles → Shell → "When the shell exits" and choose "Close the window" or "Close if the shell exited cleanly".

1
  • Thanks for the input, I have my solution from @Luka 's answer. Since this is for multiple clients (including developers) I can not go and change terminal settings. Other users are not from the it environment, so I can not expect them to launch an sh command after logging in :) Thanks anyway
    – Sevi
    Mar 16, 2017 at 14:37
0

Something more generic for using with Linux and macOS:

#!/bin/bash -e

function kill_self_terminal {
  local tk_child_pid=$1
  if [[ $tk_child_pid == 1 ]]; then
    return 1
  fi
  local tk_parent_pid=$(ps -p $tk_child_pid -o ppid=)
  if [[ $tk_child_pid == $tk_parent_pid ]]; then
    return 1
  fi
  local tk_parent_command=$(ps -p $tk_parent_pid -o comm= | tr '[:upper:]' '[:lower:]')
  if [[ -n $tk_parent_command ]] && [[ $tk_parent_command == *term* ]]; then
    kill $tk_parent_pid
  else
    kill_self_terminal $tk_parent_pid
  fi
}

kill_self_terminal $$ || {
  echo "Unable to find terminal to close"
}

If you are using different terminal commands, just change the tk_parent_command check to term part

IMPORTANT: I'm using a Debian Linux 12 so I hope this works on macOS, let me know how it goes for tuning the script

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .