This script opens three terminals, but if I hit Ctrl + C where I run this script, all three child terminals will close too!
File testChildDetach.sh
#!/bin/bash
echo $$
while true; do
nohup xterm -e "echo nohup;bash"&
xterm -e "echo disown;bash"&
disown
xterm -e "echo normal;bash"&
while true; do
ps -A -o pid,ppid,command |grep xterm |grep -v grep
echo sleep 5
sleep 5
done
done
I need to find a way to keep a child terminal open!
I can't fork the script like testChildDetach.sh&
as it will give back the command line. The real script is much more complex and must not do that.
Here is Ubuntu 12.10 64 bits (I couldn't make it work on Ubuntu 12.04 too as I remember).
See at the output that the parent of these terminals remain the same. I wonder, if the parent terminal/proccess is killed, what happens to the parent of the child processes/terminals? I would expect them to become "1".
Based on the tip given by @amrith92, I managed to improve the script to this:
testChildDetach.sh
#!/bin/bash
echo $$
function FUNCdetach() {
nohup xterm -e "echo nohup_detach;bash"&
xterm -e "echo disown_detach;bash"&
disown
xterm -e "echo normal_detach;bash"&
}
export -f FUNCdetach
while true; do
nohup xterm -e "echo nohup;bash"&
xterm -e "echo disown;bash"&
disown
xterm -e "echo normal;bash"&
xterm -e "echo FUNCdetach; FUNCdetach; bash;"&
while true; do
ps -A -o pid,ppid,command |grep xterm |grep -v grep
echo sleep 5
sleep 5
done
done
Now, when you hit Ctrl + C, 3+1 child terminals will die, but 3 will stay alive!
Also, in this case, there seems to be no difference in using nohup
, disown
or nothing...
In short, xterm -e "xterm -e \"bash\""
allows me to hit Ctrl + C and the child terminal stays open! But... I feel like I found a flaw that may be fixed in the future... So I don't feel confortable coding my scripts this way.