Phil for Humanity Phil for Humanity
A Guide for the Survival of Humankind and Helping the World, Society, and Yourself.

Waiting for Child Process in Perl

Here is a neat little Perl trick on waiting for a child process to finish.

Let's say that you want to start another process using the open command like this:

open (FILE, "sleep 30");

This command will fork a new child process; and in this case; the child process will run the “sleep 30” command. Now, if the parent process exits or close the “FILE” before the child process finishes executing like this:

open (FILE, "sleep 30");
close (FILE);

Then the child process will immediately terminate before being allowed to complete executing.

If you don't want to the child process to terminate, you can use this Perl trick.

open (FILE, " | sleep 30");
close (FILE);

Because of the pipe character, the parent process will wait to close “FILE” until the child process terminates before the Perl script continues running any other commands.

by Phil for Humanity
on 20100813

Related Articles
 » Selective UNIX Wait
 » How to Fork a Child Process and Wait in Perl
 » UNIX Support: Analyzing and Improving System Loads