in reply to Re: parent process stuck in read(2) on pipe opened to child process using backtick
in thread parent process stuck in read(2) on pipe opened to child process using backtick

You are absolutely right - can't thank you enough. I checked - sleep is holding on to the pipe that's why read(2) doesn't exit. I can get really dumb at times - how hard was that to figure out :) Also, when the child is a perl script instead of bash/sh, killing it takes away the sleep too. That explains why I saw the issue only with bash/sh. Shouldn't bash/sh do the same, i.e., take care of cleaning up it's child processes??? Maybe this is not the right forum for bash/sh but what's the harm in asking :)

  • Comment on Re^2: parent process stuck in read(2) on pipe opened to child process using backtick

Replies are listed 'Best First'.
Re^3: parent process stuck in read(2) on pipe opened to child process using backtick
by Eliya (Vicar) on Feb 14, 2012 at 15:21 UTC
    ... Maybe this is not the right forum for bash/sh but what's the harm in asking :)

    Yes, this is not a bash forum, and all in all, bash configuration is a rather complex topic...

    Anyhow, the easiest approach would probably be to add a line

    trap "kill 0" EXIT

    to your a.sh.  This sets up an exit handler which kills the current process group.

    Then run a.sh in a new process group (so you avoid killing the calling Perl script, too):

    my $x=`exec perl -e "setpgrp; exec '/root/a.sh'"`;

    (I'm not aware of any way to create a new process group from within the shell script itself, so I'm using Perl's setpgrp here.)

        This isn't really what I meant.   Or how would you adapt this in any elegant way so that when put in a.sh, it runs the code in the script itself under a new process group?  (The idea being that you could say my $x=`./a.sh` without creating an extra wrapper script.)