2

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.

2 Answers 2

3

Run the script itself with a fork &, i.e. if your script name is test.sh, run it like:

:$ ./test.sh &

Even if the parent process is killed, your spawned terminals should still be running :)

Of course, if you do it this way, the Ctrl+C will have no effect [i.e, the process will still be running in the background. I killed it with a kill -9. You could also close the shell.]

And to answer your second question, if the parent terminal/proccess is killed, then the parent id does indeed become 1. See the screenshot I've attached :) Parent process ID after killing spawner process

1
  • thx! but I cant run test.sh& because that gives back the command line. But your tip provided me the ways to find a workaround, basically I have to do this xterm -e "xterm -e \"bash\"", so now, if I hit ctrl+c the 1st xterm dies, but its child (even without fork) keeps running! The only problem seems that I feel like I found a flaw, and so, in the future this "feature" may be fixed and therefore work anymore :( Apr 29, 2013 at 21:46
0

this is a temporary solution IMHO, as I feel to be using a flaw to make it work... anyway if there is a better solution I would still like to know:

testChildDetach002.sh

#!/bin/bash 

function FUNCdoSomething() {
    while true; do 
        echo "CHILD: doing something"
        sleep 1; 
    done
}
export -f FUNCdoSomething

xterm -e "echo \"TEMP xterm...\"; xterm -e \"FUNCdoSomething\""&
# wait for the child to open
while ! ps --ppid $! 2>&1 >/dev/null; do
    sleep 1
done
kill -SIGINT $!

while true; do 
    echo "MAIN: doing something, you can hit ctrl+c here and child will keep running!";
    sleep 1; 
done;

thx again on the tip by @amrith92!

You must log in to answer this question.

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