venky4289 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am running a perl script using system command and & symbol in background from another perl script,
Now how can the child process get notified and exits immediately whenever the parent process exits.

  • Comment on how to make child process die after parent exists in perl

Replies are listed 'Best First'.
Re: how to make child process die after parent exists in perl
by salva (Canon) on Jan 09, 2014 at 13:31 UTC
    Doing that reliably is far from easy but there are several alternatives:

    Lock some file or semaphore on the parent and on every child process use a thread to wait until the lock is released. Then kill the child process from the thread.

    Allocate a new pty (IO::Pty) and set it as the children tty. Once the parent exits, children get a SIGHUP.

    On the children processes monitor the parent pid (getppid()). When it becomes 1 it means that the parent has exited. Though, for that to work you will have to use fork+exec to spawn the children instead of system("... &") in order to avoid the intermediate shell process.

Re: how to make child process die after parent exists in perl
by Anonymous Monk on Jan 09, 2014 at 12:04 UTC
Re: how to make child process die after parent exists in perl
by locked_user sundialsvc4 (Abbot) on Jan 09, 2014 at 16:29 UTC

    This seems virtually identical to this earlier PM post:   Killing child process ...

    This stackoverflow comment-thread also has useful explanations of how SIGTERM is propagated in Unix/Linux.

    Be sure that you clearly understand the difference between this signal and SIGKILL.   The graceful handling of a request to terminate is often done badly/incorrectly, resulting in unstable and awkward landings.   Proc::Background is very good “sugar” to help smooth over many rough spots.