I use a method that has some similarities to the answer of terdon (and was created with some help of him - Thank you @terdon for this!), but has a slightly different approach:
I create a temp file so that the child terminal can communicate with the parent terminal and tell it the PID of its corresponding bash instance. Then I let the parent terminal read the temp file and remember the PID, delete the file and continue by checking every 100ms (delay can be changed) whether the child terminal's bash instance is still running. If not, the terminal was closed either manually or because the command finished. Then the launching command finishes and the parent terminal is usable again.
Advantage of this approach:
All commands that will be launched (ls; sleep 3
or any replacement for those) are executed after the command that is responsible to allow detection of the terminal closing. So if the inner command hangs or you close the terminal manually before it ends, the mechanism is still working and will continue execution of the outer script instead of running into infinite loops.
The code as one-liner with debug output ("launched" after the child terminal window was opened, "terminated" after it was closed) and accuracy of 0.1 seconds is:
pidfile=$(mktemp); gnome-terminal -x bash -c "echo \$$>$pidfile; ls; sleep 3"; until [ -s $pidfile ]; do sleep 0.1; done; terminalpid=$(cat "$pidfile"); rm $pidfile; echo "launched"; while ps -p $terminalpid > /dev/null 2>&1; do sleep 0.1; done; echo "terminated"
Or without debug output:
pidfile=$(mktemp); gnome-terminal -x bash -c "echo \$$>$pidfile; ls; sleep 3"; until [ -s $pidfile ]; do sleep 0.1; done; terminalpid=$(cat "$pidfile"); rm $pidfile; while ps -p $terminalpid > /dev/null 2>&1; do sleep 0.1; done
Almost the same code but with more flexibility written as bash script:
#! /bin/bash
delay=0.1
pidfile=$(mktemp)
gnome-terminal -x bash -c "echo \$$>$pidfile; ls; sleep 3"
until [ -s $pidfile ]
do sleep $delay
done
terminalpid=$(cat "$pidfile")
rm $pidfile
echo "launched"
while ps -p $terminalpid > /dev/null 2>&1
do sleep $delay
done
echo "terminated"
You may omit the echo "launched"
and echo "terminated"
lines of course, as well as you can change the delay=0.1
line to another delay between two checks of the terminal state (in seconds) if you need it to be more or less accurate.
To execute another custom command in the child terminal, replace the line
gnome-terminal -x bash -c "echo \$$>$pidfile; ls; sleep 3"
with the following (insert your command instead of the capitalized placeholder!)
gnome-terminal -x bash -c "echo \$$>$pidfile; INSERTYOURCOMMANDSHERE"
non-asynchronous
=synchronous
?ls
command, waits 3 seconds, then closes the terminal window and returns control to the first terminal window now, so that you can't continue in the first window, while the second one is open. Correct?--disable-factory
does not work any more in the current version, although it is still listed in the manpage. Sorry.